我正在尝试将HTML转换为PDF。 在HTML中,有些符号未显示在PDF文档中。 在调试时,我发现&#8209或&#x字符在IText库中可用的任何默认字体中均不可用。 我的HTML在其字体家族中没有Arial Unicode MS,因此即使我将该字体添加到itext渲染器中也不起作用,所以我试图在创建PDF时发现我们可以使用一种字体在HTML中没有提及? HTML示例:
Sample HTMLPlease call this
number in case of emergency, 123‑456‑7890.
Java代码:
@Component
public class PDFConverter {
@Autowired
private Tidy tidy;
@Autowired
private ITextRenderer renderer;
@Autowired
private Gson gson;
public String pdfBox(byte[] bytes) {
byte[] outDoc = null;
String pdf = null;
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String html = new String(bytes);
String xhtml = toXHTML(html);
InputStream is = new ByteArrayInputStream(xhtml.getBytes("UTF-8"));
ITextFontResolver resolver = renderer.getFontResolver();
Document doc = tidy.parseDOM(is, null);
renderer.setDocument(doc, null);
resolver.addFont("*filepath*/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(outputStream);
outDoc = outputStream.toByteArray();
pdf = Base64.getEncoder().encodeToString(outDoc);
renderer.finishPDF();
outputStream.flush();
outputStream.close();
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
return pdf;
}
private String toXHTML(String html) {
final org.jsoup.nodes.Document document = org.jsoup.Jsoup.parse(html, "UTF-8");
document.outputSettings().syntax(org.jsoup.nodes.Document.OutputSettings.Syntax.xml);
document.outputSettings().prettyPrint(false);
document.outputSettings().charset("ASCII");
document.outputSettings().escapeMode(EscapeMode.xhtml);
return document.html();
}
}
itext html 转换pdf后 字体加粗 java - 如何使用ITEXTRenderer将HTML转换为PDF时设置新的不同字体 - 堆栈内存溢出...