gpt4 book ai didi

java - 打印时 PDFBox 内存不足

转载 作者:行者123 更新时间:2023-12-02 13:20:44 25 4
gpt4 key购买 nike

我在使用默认打印机直接打印 PDF 文件的软件时遇到问题。有时,当用户打印 PDF 文件时,我会收到 OutOfMemoryError,但我不知道问题出在哪里。

java.lang.OutOfMemoryError: Java heap space 
at java.awt.image.DataBufferInt.<init>(Unknown Source)
at java.awt.image.Raster.createPackedRaster(Unknown Source)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(Unknown Source)
at java.awt.image.BufferedImage.<init>(Unknown Source)
at org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray.toRGBImage(PDDeviceGray.java:78)
at org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.from1Bit(SampledImageReader.java:216)
at org.apache.pdfbox.pdmodel.graphics.image.SampledImageReader.getRGBImage(SampledImageReader.java:142)
at org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject.getImage(PDImageXObject.java:340)
at org.apache.pdfbox.rendering.PageDrawer.drawImage(PageDrawer.java:793)
at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:62)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:815)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:472)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:446)
at org.apache.pdfbox.contentstream.PDFStreamEngine.showForm(PDFStreamEngine.java:178)
at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:70)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:815)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:472)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:446)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:149)
at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:189)
at org.apache.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:208)
at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:169)
at org.apache.pdfbox.printing.PDFPrintable.print(PDFPrintable.java:184)
at sun.print.RasterPrinterJob.printPage(Unknown Source)
at sun.print.RasterPrinterJob.print(Unknown Source)

我使用此代码通过 PDFBox 加载 PDF 文件并使用 Java 进行打印。

try (PDDocument doc = PDDocument.load(fileToPrint)) {

PrinterJob job = PrinterJob.getPrinterJob();

job.setJobName(jobName);
job.setPageable(new PDFPageable(doc, Orientation.AUTO, false, 300));
job.setPrintService(printerService);

...

job.print(attributes);

...

} catch (IOException e) {
...
} catch (PrinterException e) {
...
}

使用PDFBox打印的方法

@Test
public void currentVersionTest() throws IOException, PrinterException {

String jobName = testDocumentPdf.getFileName().toString();

try (PDDocument doc = PDDocument.load(testDocumentPdf.toFile(), MemoryUsageSetting.setupTempFileOnly())) {

PrinterJob job = PrinterJob.getPrinterJob();

job.setJobName(jobName);
job.setPageable(new PDFPageable(doc, Orientation.AUTO, false, 0));
job.setPrintService(printService);

PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
attributes.add(Chromaticity.MONOCHROME);

job.print(attributes);

}

checkJobInQueue(jobName);
}

最佳答案

内存消耗取决于多个因素,例如图像数量、DPI 设置等。您可能需要调整应用程序的内存设置。

请查看https://pdfbox.apache.org/2.0/faq.html#outofmemoryerror获取一些改善内存需求/设置的建议。

关于java - 打印时 PDFBox 内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43582988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com