coral
coral

Reputation: 191

Convert PDF to PNG - Java 10 gives a different result than Java 8

I want to convert PDF to PNG file. But for some reasons, Java 10 gives a different PNG than Java 8

private static void writeImageToPath(String sourcePath, String path, int pageWidth, int pageHeight) throws IOException 
{

    File sourceFile = new File(sourcePath);
    PDDocument document = PDDocument.load(sourceFile);
    PDFRenderer renderer = new PDFRenderer(document);
    BufferedImage buff= renderer.renderImage(0, 1, ImageType.ARGB);
    File outputfile = new File(path);
    Image image = buff.getScaledInstance(pageWidth, pageHeight, Image.SCALE_SMOOTH);
    BufferedImage bufferedImage = new BufferedImage(pageWidth, pageHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufferedImage.createGraphics();
    g2d.drawImage(image, 0, 0, null);
    g2d.setColor(Color.BLACK);
    g2d.dispose();
    ImageIO.write(bufferedImage, "png", outputfile);
}

I read that the compression settings have changed in Java 9 PNG writer, so that might be why I'm seeing different results from Java 8. https://github.com/gredler/jdk9-png-writer-backport

Do you know how can I overcome this issue?

Thanks in advance!!

Upvotes: 1

Views: 439

Answers (2)

Lonzak
Lonzak

Reputation: 9816

That was part of the reason why I switched to pngj. Since that lib is stable (unless I do upgrade or change something) I get stable results. And it is ~100% faster than the old PNGWriter and still ~20% faster than the backported version of the PNGWriter. I already talked to tilman to add this code to pdfbox but I didn't have the time to do it yet.

Interestingly I disovered the problem with the imageIO PNGWriter myself and thought about patching it but then saw your link to the backported version and the integration in java9. So somebody else was faster :-)

Upvotes: 0

Tilman Hausherr
Tilman Hausherr

Reputation: 18926

tl;dr: accept it.

There are often slight differences in the rendering between jdk versions. For jdk8, the use of the Kodak CMS color management system is recommended (see Getting started) because the new LittleCMS was very slow, and Kodak CMS is no longer available in jdk10, so now Little CMS is used. The rendering result has slightly different (usually better) colors. Curve drawing may also be slightly different.

I have run pixel diff tests on PDFBox for years (to detect regressions), and I'm used to small differences. See TestPDFToImage.java in the source code... pixel difference values up to 3 are ignored.

Even with that, there are still slight differences, which make regression tests difficult. When I test PDFBox with a new java version (to see if there is anything needing attention), I do a visual inspection of the visual differences files. This takes a lot of time (these tests are done on over 1000 PDF files).

There are also visual differences between different OS, or even different computers with the same OS, because of different fonts installed.

Upvotes: 2

Related Questions