1
votes

I have problem with converting .pptx slides to .pdf document on my server. I use itextpdf 5.5.10 and apache poi 3.15 for .pptx files. If the text contains Chinese characters, the positioning of all characters is bad. On my local machine (windows 7) I have no problems. This is what it looks like on my machine

enter image description here

And this is what it looks like on the server (CentOS Linux release 7.4.1708 (Core) with ubuntu font family installed) enter image description here

This is the (java) code I use to do the conversion:

PdfContentByte canvas = writer.getDirectContent();
UnicodeFontMapper mapper = new UnicodeFontMapper();
    for (XSLFSlide slide : ppt.getSlides()) {
        PdfTemplate template = canvas.createTemplate(width, height);
        Graphics2D g2d = new PdfGraphics2D(template, width, height, mapper);
        // default rendering options
        DrawFactory.getInstance(g2d).fixFonts(g2d);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
        //Draw slide
        slide.draw(g2d);
        canvas.addTemplate(template, 0, 0);
        g2d.dispose();
        document.newPage();
    }

UnicodeFontMapper class:

public class UnicodeFontMapper extends DefaultFontMapper {

@Override
public BaseFont awtToPdf(Font font) {
    //using own fonts
    String fontFamily = "ArialUni";
    registerFontFamily(fontFamily);
    int style = com.itextpdf.text.Font.NORMAL;
    if (font.isBold()) {
        if (font.isItalic()) {
            style = com.itextpdf.text.Font.BOLDITALIC;
        } else {
            style = com.itextpdf.text.Font.BOLD;
        }
    }
    com.itextpdf.text.Font pdfFont = FontFactory.getFont(fontFamily, BaseFont.IDENTITY_H, true, font.getSize(), style);
    return pdfFont.getBaseFont();
}

I use ArialUni.ttf font. As I understand I'm missing something on my server but I can't figure out what exactly.

1

1 Answers

3
votes

Arial unicode is not present on all systems. This can cause iText to not render the characters you specified in that font. (Similary, if a font does not contain a glyph).

Optionally, if you are using the OpenJDK, you might want to look into the workings of graphics2D. Maybe you are using Oracle's version of the JDK on Windows, and OpenJDK on CentOS. Although both versions of the JDK are supposed to work in an identical way, there might be small differences in some areas, such as Graphics2D.

You can easily be check which version of the JDK you are using by running java -version