我正在尝试将HTMLWorker用于itext pdf,而对于缅甸字符则不起作用,而在纯html中,它可以工作HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href='https://mmwebfonts.comquas.com/fonts/?font=myanmar3' />
<link rel="stylesheet" href='https://mmwebfonts.comquas.com/fonts/?font=zawgyi' />
<style type="text/css">
.zawgyi{
font-family:Zawgyi-One;
}
.unicode{
font-family:Myanmar3,Yunghkio,'Masterpiece Uni Sans';
}
</style>
</head>
<body>
<h3>This is for ZawGyI font</h3>
<p class="zawgyi">၀န္ထမ္းလစာေပးေခ်လႊာ( လစာငွေ )
သည္စာသည္ ေဇာ္ဂ်ီ ျဖင့္ေရးေသာစာျဖစ္သည္
</p>
<h3>This is for unicode (myanmar3) font</h3>
<p class="unicode">၀န္ထမ္းလစာေပးေခ်လႊာ( လစာငွေ )
သည်စာသည် unicode ဖြင့်ရေးသောစာဖြစ်သည်
</p>
</body>
</html>
itext代码:
String str1="<html><head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Document</title>\n" +
" <link rel=\"stylesheet\" href='https://mmwebfonts.comquas.com/fonts/?font=myanmar3' />\n" +
" <link rel=\"stylesheet\" href='https://mmwebfonts.comquas.com/fonts/?font=zawgyi' /> \n" +
" <style type=\"text/css\">\n" +
" .zawgyi{\n" +
" font-family:Zawgyi-One;\n" +
" }\n" +
" .unicode{\n" +
" font-family:Myanmar3,Yunghkio,'Masterpiece Uni Sans';\n" +
" }\n" +
" </style>\n" +
"</head>\n" +
"<body>\n" +
" <h3>This is for ZawGyI font</h3>\n" +
" <p class=\"zawgyi\">\n" +
" သည္စာသည္ ေဇာ္ဂ်ီ ျဖင့္ေရးေသာစာျဖစ္သည္\n" +
" </p>\n" +
" <h3>This is for unicode (myanmar3) font</h3>\n" +
" <p class=\"unicode\">\n" +
" သည်စာသည် unicode ဖြင့်ရေးသောစာဖြစ်သည်\n" +
" </p>\n" +
"</body>\n" +
"</html>";
HTMLWorker worker = new HTMLWorker(document);
worker.parse(new StringReader(str1));
document.close();
[当我尝试使用XMLWorker时,它说head标签不能嵌套。当我使用FontSelector时,它可以正常工作,但是我需要解析html,并且无法将FontSelector与HTML一起使用。
这似乎是iText5的问题,我将其升级到iText7,现在可以通过以下代码正常工作:
File pdfDest = new File(pdfFile);
String html = <>html;
ConverterProperties converterProperties = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider(true, false, true);
converterProperties.setFontProvider(fontProvider);
try {
HtmlConverter.convertToPdf(html,
new FileOutputStream(pdfDest), converterProperties);
} catch (FileNotFoundException | IOException ex) {
}