You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Is it possible to create a PDF with UTF-8 character encoding? #181
@Test
fun test_parseToPdf_convertsMarkdownToPdfWithUTF8CharacterSet() {
val markdown = "Общие"
val inputStream = markdownParser.parseToPdf(markdown, "test").inputStream()
val fileText = pdfText(inputStream)
assertThat(fileText).contains("Общие")
}
private fun pdfText(input: InputStream): String? {
try {
var document = PDDocument.load(input)
val stripper = PDFTextStripper()
return stripper.getText(document)
} catch (e: Exception) {
e.printStackTrace()
}
return null
}
This is my parser class:
class MarkdownParser(private val parser: Parser,
private val htmlRenderer: HtmlRenderer) {
fun parseToHtml(markdownContent: String): String {
val document = parser.parse(markdownContent)
return htmlRenderer.render(document)
}
fun parseToPdf(markdownContent: String, path: String): ByteArray {
val options = PegdownOptionsAdapter.flexmarkOptions(
Extensions.ALL and (Extensions.ANCHORLINKS or Extensions.EXTANCHORLINKS_WRAP).inv()
).toMutable()
val html = parseToHtml(markdownContent)
val out = ByteArrayOutputStream()
PdfConverterExtension.exportToPdf(out, html, path, options)
return out.toByteArray()
}
}
Parser is com.vladsch.flexmark.parse.Parser, HtmlRenderer is com.vladsch.flexmark.html.HtmlRenderer.
As I am just passing Outputstream to the PdfConverterExtension I don't have control in writing the data. Is there a possibility to create PDF with UTF-8 Characters? The html content still has the correct HTML encoding
@shaolinh84, I am looking into it because it seems that the openhtmltopdf is not converting the characters in the HTML (taken from the String variable passed to openhtmltopdf):
<html><head><metahttp-equiv="content-type" content="text/html; charset=UTF-8"></head><body><ul><li>Test PDF with Unicode chars: Общие</li></ul></body></html>
@shaolinh84, it seems that the PDF conversion depends on the fonts which are used and whether they have the given Unicode characters.
You should skip the flexmark-java PDF converter and build your PDF conversion with the code used in the converter and add fonts available in the PDF. I have not done this yet so it is a theoretical solution.
The code in PDF converter extension is:
publicstaticvoidexportToPdf(finalOutputStreamos, finalStringhtml, finalStringurl, finalPdfRendererBuilder.TextDirectiondefaultTextDirection) {
try {
// There are more options on the builder than shown below.PdfRendererBuilderbuilder = newPdfRendererBuilder();
if (defaultTextDirection != null) {
builder.useUnicodeBidiSplitter(newICUBidiSplitter.ICUBidiSplitterFactory());
builder.useUnicodeBidiReorderer(newICUBidiReorderer());
builder.defaultTextDirection(defaultTextDirection); // OR RTL
}
org.jsoup.nodes.Documentdoc;
doc = Jsoup.parse(html);
Documentdom = DOMBuilder.jsoup2DOM(doc);
builder.withW3cDocument(dom, url);
builder.toStream(os);
builder.run();
} catch (Exceptione) {
e.printStackTrace();
// LOG exception
} finally {
try {
os.close();
} catch (IOExceptione) {
// swallow
}
}
}
The pdf renderer builder has a function to add a font to the pdf conversion, from what I understand.
A solution to the font problem is to define an embedded TrueType font in the style or stylesheet and set the body tag to use this font. OpenHtmlToPDF will use the characters from the font which has them defined.
For example including Noto Serif/Sans/Mono fonts and adding noto-serif, noto-sans and noto-mono families to CSS to allow PDF to use these for rendering text.
However, the PDF converter requires TrueType fonts and Noto CJK fonts are OpenFonts which cannot be used. The solution is to download a TrueType Unicode font that supports CJK character set and add it to the custom rendering profile to be used for PDF.
Activity
vsch commentedon Dec 8, 2017
@shaolinh84, I am looking into it because it seems that the openhtmltopdf is not converting the characters in the HTML (taken from the String variable passed to openhtmltopdf):
The resulting PDF is:
It could be some configuration that is missing.
vsch commentedon Dec 8, 2017
@shaolinh84, it seems that the PDF conversion depends on the fonts which are used and whether they have the given Unicode characters.
You should skip the flexmark-java PDF converter and build your PDF conversion with the code used in the converter and add fonts available in the PDF. I have not done this yet so it is a theoretical solution.
The code in PDF converter extension is:
The pdf renderer builder has a function to add a font to the pdf conversion, from what I understand.
A better example is in the issues for openhtmltopdf: danfickle/openhtmltopdf#129
a-reznic commentedon Dec 18, 2017
I have some isssue...
vsch commentedon Jan 24, 2019
A solution to the font problem is to define an embedded TrueType font in the style or stylesheet and set the
bodytag to use this font. OpenHtmlToPDF will use the characters from the font which has them defined.For example including Noto Serif/Sans/Mono fonts and adding
noto-serif,noto-sansandnoto-monofamilies to CSS to allow PDF to use these for rendering text.However, the PDF converter requires TrueType fonts and
Noto CJKfonts are OpenFonts which cannot be used. The solution is to download a TrueType Unicode font that supports CJK character set and add it to the custom rendering profile to be used for PDF.For my test I used
arialuni.ttffrom https://www.wfonts.com/font/arial-unicode-msIf the installation directory for the fonts is
/usr/local/fonts/then the following in the stylesheet should be added:Sample PdfConverter.java updated. Wiki Page with information added: PDF-Renderer-Converter