【问题标题】:PDF stuck in "printing" state using Java PDFBox 2.0.21PDF 使用 Java PDFBox 2.0.21 卡在“打印”状态
【发布时间】:2021-06-12 20:59:35
【问题描述】:

我正在尝试在 Java 中设置一个可以使用 PDFBox 打印 PDF 文件的打印机类。 我的 printPdf 方法成功地将 .pdf 文件添加到打印机队列中,但它根本不打印(它卡在“正在打印...”状态)。

它只发生在某些特定的 PDF 文件上。对于某些 pdf 文件,它可以完美运行,对于某些问题会发生。

这是我用来打印 pdf 文件的代码:

File file = new File("C:/Users/user/Desktop/Java Printing.pdf");
FilePrinter.printPdf(file, "Printer name");

FilePrinter.printPdf 方法:

public static void printPdf(File pdfFile, String laserName)
{
    PDDocument document = null;
    try {
        PrintRequestAttributeSet attr = new HashPrintRequestAttributeSet();
        attr.add(MediaSizeName.ISO_A4);
        attr.add(Sides.DUPLEX);
        document = PDDocument.load(pdfFile);

        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService myPrintService = PrintServiceLookup.lookupDefaultPrintService();

        for (PrintService printer : printServices) {
            if (printer.getName().equals(laserName))
                myPrintService = printer;
        }

        PrinterJob job = PrinterJob.getPrinterJob();

        job.setPageable(new PDFPageable(document));
        job.setPrintService(myPrintService);

        job.print(attr);

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally{
        if(document != null) {
            try {
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

我要打印的 pdf 文件有 2 页(它没有损坏,我可以在任何网络浏览器中打开它),但在打印机队列的文件属性中,它显示文件大小为 0 并且有 0页面(参见下一张图片)

File properties inside the printer's queue

Printer's queue status when I try to print the PDF

这个问题与 PDFBox 有关吗?到我的打印机?如果我尝试从我的网络浏览器打印它,它就像一个魅力,但我真的无法用 java 打印它。

【问题讨论】:

  • 可能是透明度,导致打印速度非常慢。尝试更改PDFPageable 构造,以便“dpi”参数是实际的 dpi 值,例如300 或 600。如果可能,请分享您的 PDF。
  • 感谢您的回答@TilmanHausherr。我尝试使用具有不同 DPI 值的 PDFPageable 构造函数。不幸的是,它仍然卡在打印线轴中。我无法共享 pdf,因为它包含非常敏感的数据。我只能说它是由扫描的文件组成的。
  • 这里提到了打印假脱机问题:issues.apache.org/jira/browse/PDFBOX-3729 它提到了一个可能会或可能不会产生影响的设置。
  • 非常感谢您的意见。我终于设法通过完全卸载来使其工作 - 重新安装打印机的驱动程序(当告诉我它们是最新的时,windows 是错误的)。

标签: java pdf printing pdfbox printers


【解决方案1】:

在完全卸载并重新安装打印机驱动程序后修复。当它告诉我它们是最新的时,Windows 是错误的!

【讨论】:

    猜你喜欢
    • 2016-06-15
    • 2013-09-09
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    • 2014-02-10
    • 2013-11-21
    相关资源
    最近更新 更多