Pdf produced using FlyingSaucer/iTExt5 is blank










0















I am trying to generate a PDF from a html string and am getting a response but the document is not displayed in the PDF viewer. I appears like I am missing something and the response is a corrupt PDF. Would highly appreciate it if I could get any suggestions.



Have included the following in my pom.xml



 <!-- dependencies for saving as PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
</dependency>


My endpoint is a controller and the code looks as below:



 @POST
@Produces("application/pdf")
@Path("/renderPDF")
public void renderPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) throws WebAppException
try
String = "<html><body>some content</body></html>";
RenderPDF.createPDF(response, sb);

catch (Exception e)
LOGGER.error("renderPDF", e);




createPDF is defined below:



public class RenderPDF {
public static void createPDF(HttpServletResponse response, String html)
throws IOException, DocumentException, ParserConfigurationException, SAXException
try
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());

OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
catch(Exception e)












share|improve this question






















  • The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.

    – user3101143
    Nov 20 '18 at 8:43















0















I am trying to generate a PDF from a html string and am getting a response but the document is not displayed in the PDF viewer. I appears like I am missing something and the response is a corrupt PDF. Would highly appreciate it if I could get any suggestions.



Have included the following in my pom.xml



 <!-- dependencies for saving as PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
</dependency>


My endpoint is a controller and the code looks as below:



 @POST
@Produces("application/pdf")
@Path("/renderPDF")
public void renderPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) throws WebAppException
try
String = "<html><body>some content</body></html>";
RenderPDF.createPDF(response, sb);

catch (Exception e)
LOGGER.error("renderPDF", e);




createPDF is defined below:



public class RenderPDF {
public static void createPDF(HttpServletResponse response, String html)
throws IOException, DocumentException, ParserConfigurationException, SAXException
try
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());

OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
catch(Exception e)












share|improve this question






















  • The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.

    – user3101143
    Nov 20 '18 at 8:43













0












0








0








I am trying to generate a PDF from a html string and am getting a response but the document is not displayed in the PDF viewer. I appears like I am missing something and the response is a corrupt PDF. Would highly appreciate it if I could get any suggestions.



Have included the following in my pom.xml



 <!-- dependencies for saving as PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
</dependency>


My endpoint is a controller and the code looks as below:



 @POST
@Produces("application/pdf")
@Path("/renderPDF")
public void renderPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) throws WebAppException
try
String = "<html><body>some content</body></html>";
RenderPDF.createPDF(response, sb);

catch (Exception e)
LOGGER.error("renderPDF", e);




createPDF is defined below:



public class RenderPDF {
public static void createPDF(HttpServletResponse response, String html)
throws IOException, DocumentException, ParserConfigurationException, SAXException
try
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());

OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
catch(Exception e)












share|improve this question














I am trying to generate a PDF from a html string and am getting a response but the document is not displayed in the PDF viewer. I appears like I am missing something and the response is a corrupt PDF. Would highly appreciate it if I could get any suggestions.



Have included the following in my pom.xml



 <!-- dependencies for saving as PDF -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-core</artifactId>
<version>9.1.16</version>
</dependency>
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.1.6</version>
</dependency>


My endpoint is a controller and the code looks as below:



 @POST
@Produces("application/pdf")
@Path("/renderPDF")
public void renderPDF(@Context HttpServletRequest request, @Context HttpServletResponse response) throws WebAppException
try
String = "<html><body>some content</body></html>";
RenderPDF.createPDF(response, sb);

catch (Exception e)
LOGGER.error("renderPDF", e);




createPDF is defined below:



public class RenderPDF {
public static void createPDF(HttpServletResponse response, String html)
throws IOException, DocumentException, ParserConfigurationException, SAXException
try
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
renderer.createPDF(baos);

// setting some response headers
response.setHeader("Expires", "0");
response.setHeader("Cache-Control",
"must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());

OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
catch(Exception e)









pdf itext pdf-generation flying-saucer






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 0:38









user3101143user3101143

5729




5729












  • The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.

    – user3101143
    Nov 20 '18 at 8:43

















  • The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.

    – user3101143
    Nov 20 '18 at 8:43
















The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.

– user3101143
Nov 20 '18 at 8:43





The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.

– user3101143
Nov 20 '18 at 8:43












1 Answer
1






active

oldest

votes


















0














The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272123%2fpdf-produced-using-flyingsaucer-itext5-is-blank%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.






    share|improve this answer



























      0














      The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.






      share|improve this answer

























        0












        0








        0







        The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.






        share|improve this answer













        The issue was in my angular http interceptor; the response type needed to be set to arrayBuffer.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 8:44









        user3101143user3101143

        5729




        5729



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272123%2fpdf-produced-using-flyingsaucer-itext5-is-blank%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo