静默打印时出现 Mac 严重错误的 PDFBox
PDFBox on Mac critical error when silent printing
我一直在尝试将我的应用程序对 PDFBox 的依赖性提升到 2.0.0 快照。
不过我遇到了一些重大问题...
所以我的代码将 PDF 作为 BASE64 字符串接收,我对其进行解码,并将生成的字节数组加载到 PDDocument 中。在我修改版本号之前,在 PDDocument 上调用 .silentPrint();
非常有效。
静默打印的实现在2.0.0改变了,我现在是这样实现的:
private Status doPdfPrint(Document document, PrintService printService) {
ByteArrayInputStream bais = null;
PDDocument doc = null;
PrinterJob printerJob = PrinterJob.getPrinterJob();
try {
printerJob.setPrintService(printService);
bais = new ByteArrayInputStream(document.getDecodedData());
doc = PDDocument.load(bais, true); //Force load
PDFPrinter pdfPrinter = new PDFPrinter(doc);
pdfPrinter.silentPrint(printerJob);
doc.close();
bais.close();
} catch (IOException | PrinterException e) {
log.warn("Failed to print! Exception occurred: {}", e.getMessage());
log.debug("EXCEPTION", e);
return Status.PRINTING_FAILED;
} finally {
IOUtils.closeQuietly(bais);
IOUtils.closeQuietly(doc);
}
return Status.PRINTING_OK;
}
但是,当 运行 在 OSX Yosemite 上执行此操作时,结果为:
2015-02-12 08:10:44.475 java[20264:1353636] Cocoa AWT: Not running on AppKit thread 0 when expected. (
0 libosxapp.dylib 0x0000000125997782 +[ThreadUtilities getJNIEnv] + 38
1 libawt_lwawt.dylib 0x000000012bf3004d syncFromJavaPixels + 1842
2 libawt_lwawt.dylib 0x000000012bf304e3 LockImage + 75
3 libawt_lwawt.dylib 0x000000012bf43040 Java_sun_java2d_CRenderer_doImage + 170
4 ??? 0x0000000108c15694 0x0 + 4441855636
5 ??? 0x0000000108c0798d 0x0 + 4441799053
)
2015-02-12 08:10:44.475 java[20264:1353636] Please file a bug report at http://java.net/jira/browse/MACOSX_PORT with this message and a reproducible test case.
2015-02-12 08:10:44.478 java[20264:1353636] java.lang.WhosebugError
at sun.java2d.CRenderer.doImage(Native Method)
at sun.java2d.OSXSurfaceData.blitImage(OSXSurfaceData.java:1027)
at sun.java2d.CRenderer.blitImage(CRenderer.java:461)
at sun.java2d.CRenderer.scaleImage(CRenderer.java:455)
at sun.java2d.CRenderer.transformImage(CRenderer.java:508)
at sun.java2d.CRenderer.transformImage(CRenderer.java:582)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3461)
at sun.print.ProxyGraphics2D.drawImage(ProxyGraphics2D.java:1315)
at org.apache.pdfbox.rendering.PageDrawer.drawBufferedImage(PageDrawer.java:755)
at org.apache.pdfbox.rendering.PageDrawer.drawImage(PageDrawer.java:719)
at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:51)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:802)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:464)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:438)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:149)
at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:160)
at org.apache.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:203)
at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:166)
at org.apache.pdfbox.printing.PDFPrinter$PDFPrintable.print(PDFPrinter.java:430)
at sun.lwawt.macosx.CPrinterJob.run(CPrinterJob.java:653)
at sun.lwawt.macosx.CPrinterJob.printToPathGraphics(CPrinterJob.java:667)
at sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
at sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:303)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1323)
at org.apache.pdfbox.printing.PDFPrinter.print(PDFPrinter.java:250)
at org.apache.pdfbox.printing.PDFPrinter.silentPrint(PDFPrinter.java:182)
at com.memnon.printr.PrintHandler.doPdfPrint(PrintHandler.java:123)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:87)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:77)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:48)
at com.memnon.printr.communication.DocumentResponseHandler.handleSuccessful(DocumentResponseHandler.java:78)
at com.memnon.printr.communication.DocumentResponseHandler.handle(DocumentResponseHandler.java:53)
at com.memnon.printr.messaging.PrintConsumer.executePrinterJob(PrintConsumer.java:62)
at com.memnon.printr.messaging.PrintConsumer.run(PrintConsumer.java:44)
at java.lang.Thread.run(Thread.java:745)
2015-02-12 08:10:44.493 java[20264:1353636] NSAlert is being used from a background thread, which is not safe. This is probably going to crash sometimes. Break on void _NSAlertWarnUnsafeBackgroundThreadUsage() to debug. This will be logged only once. This may break in the future.
2015-02-12 08:10:46.639 java[20264:1353636] Apple AWT Internal Exception: Printing failed because PMSessionEndDocumentNoDialog() returned -30871.
2015-02-12 08:10:46.639 java[20264:1353636] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Printing failed because PMSessionEndDocumentNoDialog() returned -30871.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8895c66c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff890e876e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8895c51d +[NSException raise:format:] + 205
3 AppKit 0x00007fff8d117e80 -[NSPrintSpoolingGraphicsContext dealloc] + 43
4 libobjc.A.dylib 0x00007fff890fb89c _ZN11objc_object17sidetable_releaseEb + 236
5 libobjc.A.dylib 0x00007fff890e1e8f _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 575
6 CoreFoundation 0x00007fff88834302 _CFAutoreleasePoolPop + 50
7 libawt_lwawt.dylib 0x000000012bf25fa4 Java_sun_lwawt_macosx_CPrinterJob_printLoop + 165
8 ??? 0x0000000108c15694 0x0 + 4441855636
9 ??? 0x0000000108c07160 0x0 + 4441796960
10 ??? 0x0000000108c0798d 0x0 + 4441799053
)
libc++abi.dylib: terminating with uncaught exception of type NSException
知道发生了什么事吗?
此致
所以我在这第一次发生 2 年后在这里发帖..
它还在发生。但是,这是一个 Java 错误,而不是 PDFBox 错误。因此,在 PDFBox 问题跟踪器上提交错误报告后,我得到了在打印时为文档设置 DPI 的建议。瞧,它起作用了。但是,要计算出 pdf 文档的 DPI 几乎是不可能的。
但是,如果您知道 DPI 是什么,您可以简单地这样做:
final int rasterizedDpi = 203;
final PDDocument pdfDocument = PDDocument.load(data);
final PrinterJob job = PrinterJob.getPrinterJob();
final PDFPageable pageable = new PDFPageable(pdfDocument, Orientation.AUTO, false, rasterizedDpi);
job.setPageable(pageable);
job.print();
这里的提示是 PDFPageable
的第 4 个参数,dpi...
在 Oracle 修复此 JDK 错误之前,我想我们将不得不调用此方法:)
我一直在尝试将我的应用程序对 PDFBox 的依赖性提升到 2.0.0 快照。 不过我遇到了一些重大问题...
所以我的代码将 PDF 作为 BASE64 字符串接收,我对其进行解码,并将生成的字节数组加载到 PDDocument 中。在我修改版本号之前,在 PDDocument 上调用 .silentPrint();
非常有效。
静默打印的实现在2.0.0改变了,我现在是这样实现的:
private Status doPdfPrint(Document document, PrintService printService) {
ByteArrayInputStream bais = null;
PDDocument doc = null;
PrinterJob printerJob = PrinterJob.getPrinterJob();
try {
printerJob.setPrintService(printService);
bais = new ByteArrayInputStream(document.getDecodedData());
doc = PDDocument.load(bais, true); //Force load
PDFPrinter pdfPrinter = new PDFPrinter(doc);
pdfPrinter.silentPrint(printerJob);
doc.close();
bais.close();
} catch (IOException | PrinterException e) {
log.warn("Failed to print! Exception occurred: {}", e.getMessage());
log.debug("EXCEPTION", e);
return Status.PRINTING_FAILED;
} finally {
IOUtils.closeQuietly(bais);
IOUtils.closeQuietly(doc);
}
return Status.PRINTING_OK;
}
但是,当 运行 在 OSX Yosemite 上执行此操作时,结果为:
2015-02-12 08:10:44.475 java[20264:1353636] Cocoa AWT: Not running on AppKit thread 0 when expected. (
0 libosxapp.dylib 0x0000000125997782 +[ThreadUtilities getJNIEnv] + 38
1 libawt_lwawt.dylib 0x000000012bf3004d syncFromJavaPixels + 1842
2 libawt_lwawt.dylib 0x000000012bf304e3 LockImage + 75
3 libawt_lwawt.dylib 0x000000012bf43040 Java_sun_java2d_CRenderer_doImage + 170
4 ??? 0x0000000108c15694 0x0 + 4441855636
5 ??? 0x0000000108c0798d 0x0 + 4441799053
)
2015-02-12 08:10:44.475 java[20264:1353636] Please file a bug report at http://java.net/jira/browse/MACOSX_PORT with this message and a reproducible test case.
2015-02-12 08:10:44.478 java[20264:1353636] java.lang.WhosebugError
at sun.java2d.CRenderer.doImage(Native Method)
at sun.java2d.OSXSurfaceData.blitImage(OSXSurfaceData.java:1027)
at sun.java2d.CRenderer.blitImage(CRenderer.java:461)
at sun.java2d.CRenderer.scaleImage(CRenderer.java:455)
at sun.java2d.CRenderer.transformImage(CRenderer.java:508)
at sun.java2d.CRenderer.transformImage(CRenderer.java:582)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:3461)
at sun.print.ProxyGraphics2D.drawImage(ProxyGraphics2D.java:1315)
at org.apache.pdfbox.rendering.PageDrawer.drawBufferedImage(PageDrawer.java:755)
at org.apache.pdfbox.rendering.PageDrawer.drawImage(PageDrawer.java:719)
at org.apache.pdfbox.contentstream.operator.graphics.DrawObject.process(DrawObject.java:51)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(PDFStreamEngine.java:802)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(PDFStreamEngine.java:464)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(PDFStreamEngine.java:438)
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(PDFStreamEngine.java:149)
at org.apache.pdfbox.rendering.PageDrawer.drawPage(PageDrawer.java:160)
at org.apache.pdfbox.rendering.PDFRenderer.renderPage(PDFRenderer.java:203)
at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(PDFRenderer.java:166)
at org.apache.pdfbox.printing.PDFPrinter$PDFPrintable.print(PDFPrinter.java:430)
at sun.lwawt.macosx.CPrinterJob.run(CPrinterJob.java:653)
at sun.lwawt.macosx.CPrinterJob.printToPathGraphics(CPrinterJob.java:667)
at sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
at sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:303)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1323)
at org.apache.pdfbox.printing.PDFPrinter.print(PDFPrinter.java:250)
at org.apache.pdfbox.printing.PDFPrinter.silentPrint(PDFPrinter.java:182)
at com.memnon.printr.PrintHandler.doPdfPrint(PrintHandler.java:123)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:87)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:77)
at com.memnon.printr.PrintHandler.print(PrintHandler.java:48)
at com.memnon.printr.communication.DocumentResponseHandler.handleSuccessful(DocumentResponseHandler.java:78)
at com.memnon.printr.communication.DocumentResponseHandler.handle(DocumentResponseHandler.java:53)
at com.memnon.printr.messaging.PrintConsumer.executePrinterJob(PrintConsumer.java:62)
at com.memnon.printr.messaging.PrintConsumer.run(PrintConsumer.java:44)
at java.lang.Thread.run(Thread.java:745)
2015-02-12 08:10:44.493 java[20264:1353636] NSAlert is being used from a background thread, which is not safe. This is probably going to crash sometimes. Break on void _NSAlertWarnUnsafeBackgroundThreadUsage() to debug. This will be logged only once. This may break in the future.
2015-02-12 08:10:46.639 java[20264:1353636] Apple AWT Internal Exception: Printing failed because PMSessionEndDocumentNoDialog() returned -30871.
2015-02-12 08:10:46.639 java[20264:1353636] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Printing failed because PMSessionEndDocumentNoDialog() returned -30871.'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff8895c66c __exceptionPreprocess + 172
1 libobjc.A.dylib 0x00007fff890e876e objc_exception_throw + 43
2 CoreFoundation 0x00007fff8895c51d +[NSException raise:format:] + 205
3 AppKit 0x00007fff8d117e80 -[NSPrintSpoolingGraphicsContext dealloc] + 43
4 libobjc.A.dylib 0x00007fff890fb89c _ZN11objc_object17sidetable_releaseEb + 236
5 libobjc.A.dylib 0x00007fff890e1e8f _ZN12_GLOBAL__N_119AutoreleasePoolPage3popEPv + 575
6 CoreFoundation 0x00007fff88834302 _CFAutoreleasePoolPop + 50
7 libawt_lwawt.dylib 0x000000012bf25fa4 Java_sun_lwawt_macosx_CPrinterJob_printLoop + 165
8 ??? 0x0000000108c15694 0x0 + 4441855636
9 ??? 0x0000000108c07160 0x0 + 4441796960
10 ??? 0x0000000108c0798d 0x0 + 4441799053
)
libc++abi.dylib: terminating with uncaught exception of type NSException
知道发生了什么事吗? 此致
所以我在这第一次发生 2 年后在这里发帖..
它还在发生。但是,这是一个 Java 错误,而不是 PDFBox 错误。因此,在 PDFBox 问题跟踪器上提交错误报告后,我得到了在打印时为文档设置 DPI 的建议。瞧,它起作用了。但是,要计算出 pdf 文档的 DPI 几乎是不可能的。
但是,如果您知道 DPI 是什么,您可以简单地这样做:
final int rasterizedDpi = 203;
final PDDocument pdfDocument = PDDocument.load(data);
final PrinterJob job = PrinterJob.getPrinterJob();
final PDFPageable pageable = new PDFPageable(pdfDocument, Orientation.AUTO, false, rasterizedDpi);
job.setPageable(pageable);
job.print();
这里的提示是 PDFPageable
的第 4 个参数,dpi...
在 Oracle 修复此 JDK 错误之前,我想我们将不得不调用此方法:)