Apache PDFBox拒绝打开临时创建的PDF文件 [英] Apache PDFBox refuses to open temporary created PDF file

查看:282
本文介绍了Apache PDFBox拒绝打开临时创建的PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建用于查看PDF文件的桌面JavaFX应用程序. PDF位于资源文件夹中.我将资源文件读取为流,然后创建临时文件,并使用它将内容转换为图像并显示为ImageView.

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");

一切正常(我在jar所在的目录中创建了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

并获得例外:

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)

采用这种方法:

@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;
}

在解决此问题或为我提供直接从jar读取文件的方式以避免创建临时副本的方式方面的帮助,将不胜感激.

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

推荐答案

如何创建临时文件?

常用方法在文件系统中创建一个空文件.这将在这里绊倒您的逻辑:

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

if (!currentPdf.exists()) {

据说该检查是为了避免覆盖现有文件,但是在这种情况下,您必须摆脱它.照原样,您将跳过PDF生成代码,并尝试读取一个空文件.

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.

这篇关于Apache PDFBox拒绝打开临时创建的PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆