Incorrect character spacing and breaks in xdocreport reports with cyrillic symbols

386 views Asked by At

I would like to convert a docx template to pdf with the insertion of variable values, everything works correctly with English characters, despite this, Cyrillic characters are stuck together in one line, as an incorrect value for the character spacing will be set in them, I tried to change it through pdfWriter.setSpaceCharRatio, expected gave no result.

In docx formatting is correct:

enter image description here

Method in service:

    public static void generateReport(InputStream docxInputStream, OutputStream pdfOutputStream) throws Exception {
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(docxInputStream, TemplateEngineKind.Freemarker);
        IContext context = report.createContext();

        Options options = Options.getFrom(DocumentKind.DOCX).via(ConverterTypeVia.XWPF).to(ConverterTypeTo.PDF);

        PdfOptions pdfOptions = PdfOptions.create()
                .fontEncoding("windows-1251")
                .fontProvider((s, s1, v, i, color) -> FontFactory.getFont(FontFactory.TIMES_ROMAN, "windows-1251", v, i, color));

        options.subOptions(pdfOptions);
        report.convert(context, options, pdfOutputStream);
    }

Invoke:

 FileInputStream docxInputStream = new FileInputStream("/abspath/resources/template.docx");
 FileOutputStream pdfOutputStream = new FileOutputStream("/abspath/resources/output.pdf");
 ReportGenerator.generateReport(docxInputStream, pdfOutputStream);
 docxInputStream.close();
 pdfOutputStream.close();

Pom deps:

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>xdocreport</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.32</version>
</dependency>
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity</artifactId>
    <version>1.7</version>
</dependency>
<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>

<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.poi.xwpf.converter.pdf</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>

Got incorrect formatting result:

enter image description here

How can I fix it?

1

There are 1 answers

0
Яков Клюев On

Answer: use external cyrillic fonts

InputStream fontStream = Application.class.getResourceAsStream("Arial.ttf");
BaseFont baseFont = BaseFont.createFont("Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    
PdfOptions pdfOptions = PdfOptions.create()
  .fontEncoding("windows-1251")
  .fontProvider((s, s1, v, i, color) -> FontFactory.getFont("Arial.ttf", "windows-1251", v, i, color));