Home > OS >  DOCX to PDF conversion using Opensagres and Apache poi results in missing symbols when running on Do
DOCX to PDF conversion using Opensagres and Apache poi results in missing symbols when running on Do

Time:10-16

I am using this code to convert docx to pdf:

try {
        PdfOptions options = PdfOptions.create();
        PdfConverter.getInstance().convert(firstDoc, out, options);
    } catch (IOException ex) {
        throw new IllegalArgumentException("Conversion failed.");
    }
    return prepareResponseEntity(out);

It works well on Windows, but when it's run on docker, the conversion skips symbols like "ąčęėįšųūž" and cyrillic.

I tried embedding fonts into the template. Changing the .docx template to Liberation fonts "sans" and importing its' ttf files, but nothing works. I can't install Microsoft fonts, because this app is used for commercial purposes.

What are my options?

CodePudding user response:

I found a solution:

try {
        PdfOptions options = PdfOptions.create();
        options.fontProvider((familyName, encoding, size, style, color) -> {
            try {
                BaseFont baseFont = BaseFont.createFont("fonts/LiberationSans-Regular.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
                return new Font(baseFont, size, style, color);
            } catch (Exception e) {
                throw new IllegalArgumentException("Font was not found"   e);
            }
        });
        PdfConverter.getInstance().convert(firstDoc, out, options);
    } catch (IOException ex) {
        throw new IllegalArgumentException("Conversion failed");
    }
    return prepareResponseEntity(out);

You also need to add your font .ttf file to the resource folder and it works!

  • Related