Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 飞碟不会以PDF格式呈现PNG文件_Java_Pdf Generation_Flying Saucer_Xhtmlrenderer - Fatal编程技术网

Java 飞碟不会以PDF格式呈现PNG文件

Java 飞碟不会以PDF格式呈现PNG文件,java,pdf-generation,flying-saucer,xhtmlrenderer,Java,Pdf Generation,Flying Saucer,Xhtmlrenderer,我正试图使用飞碟(9.0.7)库从XHTML生成一个PDF文件。文本和表格按预期呈现,但最终PDF中未显示图像(PNG)。图像由绝对文件路径引用(例如)。如果我改为使用URL,图像可以正确地放置在PDF中,但如何使用绝对文件路径完成工作 我的java代码如下所示: private void createPDF(String filePath) { try { File pdfFile = new File(filePath); ITextRenderer

我正试图使用飞碟(9.0.7)库从XHTML生成一个PDF文件。文本和表格按预期呈现,但最终PDF中未显示图像(PNG)。图像由绝对文件路径引用(例如
)。如果我改为使用URL,图像可以正确地放置在PDF中,但如何使用绝对文件路径完成工作

我的java代码如下所示:

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());
     }
}

任何帮助都将不胜感激。

正如Paulo Soares所提到的,使用file:///就可以了。除此之外,还必须避免在文件路径中出现反斜杠。

使用file:///会有帮助吗?是的,你是对的。非常感谢。但是,还必须避免在文件路径中使用反斜杠。@rOb我发现这个问题和注释非常有用。也许你可以为你的问题写一个答案并接受它?