Hello,
This is loosely related to the thread here: Font Issues with content containing Arabic and Chinese when saving to PDF on Linux
The issues from the original thread regarding improper font have all been resolved, for which I thank you for your help in that matter. However, it was brought to my attention that if we add an svg containing Arabic to a pdf from a Linux environment, the Arabic word order is reversed. The rest of the text in the pdf maintains the correct order. Additionally, pdfs generated in a windows environment do not show this behavior and work as expected, as do Docx files generated in both Linux and Windows environments.
Here is a docx file generated on the Linux environment:
Process Narrative - الاوامر الثابتة (1) (4).docx (26.4 KB)
Here is the INCORRECT pdf generated on Linux, showing the Arabic word order changed:
Process Narrative - الاوامر الثابتة (1) (14).pdf (95.0 KB)
For comparison, here is a CORRECT pdf generated on Windows, showing correct order:
WINDOWS_Process Narrative - الاوامر الثابتة (1).pdf (146.2 KB)
For example, notice that in the diagram svg, الاوامر اصدار appears instead of اصدار الاوامر, in addition to other order changes (the text under ‘Shape List’ and elsewhere in the pdf has remained in the correct order; only the SVG is affected).
Here is the content of the svg before it is added to the document:
svgAsString.pdf (94.0 KB)
And finally, here is the basic flow of our code (same as from the other thread, with the addition of fallback and substitution settings and harfbuzz as was recommended):
downloadPDF(String svg)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
if (SystemUtils.IS_OS_LINUX)
{
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getFontConfigSubstitution().setEnabled(true);
fontSettings.getSubstitutionSettings().getTableSubstitution().load(
this.getClass().getResourceAsStream("/substitutionSettings.xml")
);
fontSettings.getFallbackSettings().load(
this.getClass().getResourceAsStream("fallbackSettings.xml")
);
builder.getDocument().setFontSettings(fontSettings);
}
builder.insertHtml(someHtmlGoesHere, true);
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
builder.insertImage(svg.getBytes(StandardCharsets.UTF_8));
builder.insertBreak(BreakType.PARAGRAPH_BREAK);
builder.insertHtml(someOtherHtmlGoesHere, true);
doc.getLayoutOptions().setTextShaperFactory(HarfBuzzTextShaperFactory.getInstance());
SaveOptions saveOptions = DocSaveOptions.createSaveOptions(SaveFormat.PDF);
// SaveFormat.DOCX for word version
doc.save(baos, saveOptions);
return baos.toByteArray();
}
I would appreciate any assistance or advice you can offer regarding this issue. Please let me know if you need any more information.
Best regards,
Avery Norris