Apache PDFBox refuses to open temporary created PDF file

断了今生、忘了曾经 提交于 2021-01-28 18:51:08

问题


I am creating desktop JavaFX application for viewing PDF files. PDFs are at resource folder. I read resourse file as stream, then I create temporary file and use it to convert contents to image and show into ImageView.

       currentPdf =  new File("current.pdf");
        if (!currentPdf.exists()) {
            // In JAR
            InputStream inputStream = ClassLoader.getSystemClassLoader()
                    .getResourceAsStream("PDFSample.pdf");
            // Copy file
            OutputStream outputStream;
            try {
                outputStream = new FileOutputStream(currentPdf);
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
            byte[] buffer = new byte[1024];
            int length;
            try {
                while ((length = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, length);
                }
                outputStream.close();
                inputStream.close();
            } catch(IOException e) {
                throw new RuntimeException(e);
            }
        }

The problem is: when I create regular file with

currentPdf =  new File("current.pdf");

Everything works as expected (i get current.pdf created at directory where jar is located). But I want the file to be created at system temp folder and to be deleted on exit from application. I tried this:

try {
    currentPdf =  File.createTempFile("current",".pdf");
} catch (IOException e) {
    throw new RuntimeException(e);
}
currentPdf.deleteOnExit();//also tried to comment this line

And get exception:

Caused by: java.io.IOException: Error: End-of-File, expected line
    at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1517)
    at org.apache.pdfbox.pdfparser.PDFParser.parseHeader(PDFParser.java:360)
    at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:186)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1227)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1194)
    at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1165)
    at ua.com.ethereal.pdfquiz.Controller.getPdfPageAsImage(Controller.java:147)

At this method:

@SuppressWarnings("unchecked")
public static Image getPdfPageAsImage(File pdfFile, int pageNum) {
    Image convertedImage;
    try {
        PDDocument document = PDDocument.load(pdfFile);
        List<PDPage> list = document.getDocumentCatalog().getAllPages();
        PDPage page = list.get(pageNum);
        BufferedImage image = page.convertToImage(BufferedImage.TYPE_INT_RGB, 128);
        convertedImage = SwingFXUtils.toFXImage(image, null);
        document.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return convertedImage;
}

Would appreciate help in resolving this or pointing me on way to read file directly from jar to avoid creating temporary copies.


回答1:


How do you create the temporary file?

The usual methods make an empty file in the filesystem. That will trip your logic here:

if (!currentPdf.exists()) {

That check is supposedly there to avoid overwriting exiting files, but in this case you have to get rid of it. As it is, you skip your PDF generation code and try to read an empty file.



来源:https://stackoverflow.com/questions/34734829/apache-pdfbox-refuses-to-open-temporary-created-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!