javapdf-generationflying-saucerxhtmlrenderer

Flying Saucer does not render PNG files in PDF


I'm trying to generate a PDF file from XHTML by using the Flying Saucer (9.0.7) library. Texts and tables are rendered as expected but images (PNGs) are not shown in the final PDF. The images are referenced by absolute file paths (e.g.<img alt="img" src="C:\pic.png" width="300px"></img>). If I use URLs instead, the images can be placed correctly in the PDF but how can I get the job done with absolute file paths?

My java code is the following:

private void createPDF(String filePath) {
    try {
        File pdfFile = new File(filePath);

        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(pdfFile);

        OutputStream pdfFOS = new FileOutputStream(new  File(filePath.replace(".html", ".pdf")));

        renderer.layout();
        renderer.createPDF(pdfFOS);

       pdfFOS.close();
     } catch (Exception e) {
       System.err.println("Error creating pdf file: " + e.getMessage());
     }
}

Any help would be appreciated.


Solution

  • As Paulo Soares mentioned, using file:/// will do the trick. In addition to that, it's necessary to avoid backslashes in file path.