cancel
Showing results for 
Search instead for 
Did you mean: 

Display dynamic PDF directly in the browser

ericfrigot
Champ in-the-making
Champ in-the-making
Hi All,

I am trying to write a JAVA Web Script to display generated PDF in the browser. Here is the code :


public class MyPDFWebScript extends AbstractWebScript {

   @Override
   public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
         res.setContentType("application/pdf");
         res.setContentEncoding(null);
         res.addHeader("Content-Disposition", "inline;filename=12F002340.PDF");
         res.addHeader("Content-Length", Integer.toString(content.length));
         res.addHeader("Pragma", "no-cache");

         res.getWriter().write(new String(content)); // Here I write my PDF content
   }
}

When I write the content byte array into a file, I can correctly open and view the PDF
When I write the same content in the WebScriptResponse I get an empty PDF

I think something is missing into my code, but I don't know what.

Somebody could help me ?

Thanks,
Eric
1 REPLY 1

ericfrigot
Champ in-the-making
Champ in-the-making
I finally found a solution. It was an encoding problem. The code to write the pdf content is :

res.getOutputStream().write(content);

The writer must not be used for binary content