In action method (JSF) i have something like below:
public String getFile() {
byte[] pdfData = ...
// how to return byte[] as file to web browser user ?
}
How to send byte[] as pdf to browser ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the action method you can obtain the HTTP servlet response from under the JSF hoods by
ExternalContext#getResponse(). Then you need to set at least the HTTPContent-Typeheader toapplication/pdfand the HTTPContent-Dispositionheader toattachment(when you want to pop a Save As dialogue) or toinline(when you want to let the webbrowser handle the display itself). Finally, you need to ensure that you callFacesContext#responseComplete()afterwards to avoidIllegalStateExceptions flying around.Kickoff example:
That said, if you have the possibility to get the PDF content as an
InputStreamrather than abyte[], I would recommend to use that instead to save the webapp from memory hogs. You then just write it in the well-knownInputStream–OutputStreamloop the usual Java IO way.