Robin Jonsson Robin Jonsson - 1 year ago 139
Java Question

PDFBox on Mac critical error when silent printing

I have been experimenting with bumping my applications dependency on PDFBox to the 2.0.0 snapshot.
I'm having some major issues with it though...

So my code recieves a PDF as a BASE64 String, i decode it, and load the resulting bytearray into a PDDocument. Before I bumped the version number, calling

on the PDDocument worked like a charm.

The implementation of silent printing changed in 2.0.0, and I now do it this way:

private Status doPdfPrint(Document document, PrintService printService) {
ByteArrayInputStream bais = null;
PDDocument doc = null;
PrinterJob printerJob = PrinterJob.getPrinterJob();

try {
bais = new ByteArrayInputStream(document.getDecodedData());

doc = PDDocument.load(bais, true); //Force load

PDFPrinter pdfPrinter = new PDFPrinter(doc);

} catch (IOException | PrinterException e) {
log.warn("Failed to print! Exception occurred: {}", e.getMessage());
log.debug("EXCEPTION", e);
return Status.PRINTING_FAILED;
} finally {
return Status.PRINTING_OK;

However, when running this on OSX Yosemite, this results in:

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 with this message and a reproducible test case.
2015-02-12 08:10:44.478 java[20264:1353636] java.lang.StackOverflowError
at sun.java2d.CRenderer.doImage(Native Method)
at sun.java2d.OSXSurfaceData.blitImage(
at sun.java2d.CRenderer.blitImage(
at sun.java2d.CRenderer.scaleImage(
at sun.java2d.CRenderer.transformImage(
at sun.java2d.CRenderer.transformImage(
at sun.java2d.SunGraphics2D.drawImage(
at sun.print.ProxyGraphics2D.drawImage(
at org.apache.pdfbox.rendering.PageDrawer.drawBufferedImage(
at org.apache.pdfbox.rendering.PageDrawer.drawImage(
at org.apache.pdfbox.contentstream.PDFStreamEngine.processOperator(
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStreamOperators(
at org.apache.pdfbox.contentstream.PDFStreamEngine.processStream(
at org.apache.pdfbox.contentstream.PDFStreamEngine.processPage(
at org.apache.pdfbox.rendering.PageDrawer.drawPage(
at org.apache.pdfbox.rendering.PDFRenderer.renderPage(
at org.apache.pdfbox.rendering.PDFRenderer.renderPageToGraphics(
at org.apache.pdfbox.printing.PDFPrinter$PDFPrintable.print(
at sun.lwawt.macosx.CPrinterJob$
at sun.lwawt.macosx.CPrinterJob.printToPathGraphics(
at sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
at sun.lwawt.macosx.CPrinterJob.print(
at sun.print.RasterPrinterJob.print(
at org.apache.pdfbox.printing.PDFPrinter.print(
at org.apache.pdfbox.printing.PDFPrinter.silentPrint(
at com.memnon.printr.PrintHandler.doPdfPrint(
at com.memnon.printr.PrintHandler.print(
at com.memnon.printr.PrintHandler.print(
at com.memnon.printr.PrintHandler.print(
at com.memnon.printr.communication.DocumentResponseHandler.handleSuccessful(
at com.memnon.printr.communication.DocumentResponseHandler.handle(
at com.memnon.printr.messaging.PrintConsumer.executePrinterJob(
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

Any idea what's going on?

Answer Source

So I'm posting here 2 years after this occured for the first time..

It's still occuring. However, it's a Java bug, rather then a PDFBox bug. So after filing my bugreport on PDFBox issue-tracker, I got the advice to set a DPI for the document when doing printing. And voila it works. However, it's nearly impossible to figure out the DPI of a pdf document.

However, if you know what the DPI is, you can simply do this:

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

The hint here is the 4th argument to PDFPageable, the dpi...

Until Oracle fixes this JDK bug, i guess we're going to have to call this method :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download