We get to find some intriguing bugs in Java, and today I stumbled across an interesting print issue. This exception was being thrown in Java Printing
java.lang.IllegalArgumentException: Width (3) and height (0) must be > 0
at java.awt.image.SampleModel.<init>(SampleModel.java:108)
at java.awt.image.ComponentSampleModel.<init>(ComponentSampleModel.java:128)
at java.awt.image.PixelInterleavedSampleModel.<init>(PixelInterleavedSampleModel.java:69)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:638)
at java.awt.image.Raster.createInterleavedRaster(Raster.java:265)
at java.awt.image.BufferedImage.<init>(BufferedImage.java:368)
at sun.print.PSPathGraphics.redrawRegion(PSPathGraphics.java:626)
at sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:1974)
at sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1435)
at sun.print.UnixPrintJob.pageableJob(UnixPrintJob.java:643)
at sun.print.UnixPrintJob.print(UnixPrintJob.java:459)
As the exception was happening deep inside the JDK and not our code it was a rather difficult issue to hunt down. We had to step through the PDF commands until we identified the one causing the issue.
It turned out that Java printing was being passed a tiny (8×1 pixels) image. This was being scaled down with an AffineTransform to produce a final image with a height less than 1. As Java cannot handle fractions of pixels, this was being truncated to zero and causing the issue. Adding some code to ignore the image removed the issue.
This post is part of our “Printing Articles Index” in these articles, we aim to help you understand printing in Java and PDF’s.
Do you need to solve any of these problems in Java?
Convert PDF to HTML5 | Convert PDF to SVG | View Forms in the browser |
View PDF Documents | Convert PDF to image | Extract Text from PDF |
Read/Write images | Replace ImageIO | Convert Image to PDF |