Poliakoff
Poliakoff

Reputation: 1672

Apache PDFBox refuses to open temporary created PDF file

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.

Upvotes: 1

Views: 4158

Answers (1)

Thilo
Thilo

Reputation: 262494

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.

Upvotes: 3

Related Questions