Memphys
Memphys

Reputation: 101

Rendering PDF to image with PDFBox properly

So, in this application we're using iText to fill out PDF forms and PDFBox to load that filled out PDF and convert to image into our system.

The problem is when the image is converted. All the information is there, but the checkboxes are... weird? Instead of the styled checkbox "check mark" that is set on the PDF, the checkboxes get a weird "empty box" inside of them.

enter image description here

How it is supposed to be:

enter image description here

PDFBox version 2.0.11 iText version is 5.5.13

Here is a little snippet of the code where the conversion happens:

PDDocument pdf = PDDocument.load(byteArrayInputStream);
PDFRenderer renderer = new PDFRenderer(pdf);
BufferedImage[] images = new BufferedImage[pdf.getNumberOfPages()];
PDPage page = null;
BufferedImage image = null;
for (int i = 0; i < images.length; i++) {
        try {
            image = renderer.renderImageWithDPI(i, 300,org.apache.pdfbox.rendering.ImageType.RGB);
            ...

I'm kind of sensing a "loss of quality" too after the conversion. Before, we were using PDFBox 1.8 and the conversion quality was low and it was losing some font formatting and style. Since the upgrade it got better, but is still bugged.

Where the filling happens:

PdfReader reader = new PdfReader(filePath);

ByteArrayOutputStream lStr = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, lStr);
AcroFields acroFields = stamper.getAcroFields();

for (Entry<String, Item> map : acroFields.getFields().entrySet()) {
    String key = map.getKey();

    if (!fields.has(key))
        continue;

    if (fields.isNull(key))
        continue;

    acroFields.setField(key, fields.getString(key), true);
}
stamper.setFormFlattening(true);

stamper.close();
reader.close();

...

Do you guys know what this is?

Thanks!

Upvotes: 2

Views: 2660

Answers (1)

Memphys
Memphys

Reputation: 101

Got it working thanks to Tilman Hausherr's suggestion. The problem was indeed the fonts missing in the server running the application. (Zapf Dingbats and/or MS Gothic).

Installing the missing fonts in a directory "./fonts" or "/usr/share/fonts" (Linux) / "/Windows/Fonts" (Windows) did the trick!

Upvotes: 3

Related Questions