我的软件问题是直接使用默认打印机打印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)
我使用此代码加载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);
}
答案 0 :(得分:1)
内存消耗取决于多种因素,如图像数量,DPI设置等。您可能需要调整应用程序的内存设置。
请查看https://pdfbox.apache.org/2.0/faq.html#outofmemoryerror以获取一些改善内存需求/设置的建议。