gpt4 book ai didi

java - 如何使用 iText 和 XMLWorker 在 HTML 到 pdf 转换期间呈现特殊字符?

转载 作者:行者123 更新时间:2023-12-02 01:33:50 25 4
gpt4 key购买 nike

您好,我正在使用 iText 和 XMLWorker 进行 HTML 到 pdf 的转换(Java),如下所示

    public void convertHtmlToPdf(StringBuilder content, String path) throws Exception {
String methodName = "convertHtmlToPdf";

try {

XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
fontProvider.register("C:/Users/Aaryan/Downloads/arial.ttf");

final OutputStream file = new FileOutputStream(new File(path));
final Document document = new Document();
final PdfWriter writer = PdfWriter.getInstance(document, file);
document.open();


final TagProcessorFactory tagProcessorFactory = Tags.getHtmlTagProcessorFactory();
tagProcessorFactory.removeProcessor(HTML.Tag.IMG);
tagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG);

final CssFilesImpl cssFiles = new CssFilesImpl();
cssFiles.add(XMLWorkerHelper.getInstance().getDefaultCSS());
final StyleAttrCSSResolver cssResolver = new StyleAttrCSSResolver(cssFiles);
final HtmlPipelineContext hpc = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
hpc.setAcceptUnknown(true).autoBookmark(true).setTagFactory(tagProcessorFactory);
final HtmlPipeline htmlPipeline = new HtmlPipeline(hpc, new PdfWriterPipeline(document, writer));
final Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
final XMLWorker worker = new XMLWorker(pipeline, true);
final Charset charset = Charset.forName("UTF-8");
final XMLParser xmlParser = new XMLParser(true, worker, charset);

InputStream is2 = new ByteArrayInputStream(content.toString().getBytes());

xmlParser.parse(is2, charset);

is2.close();
document.close();
file.close();

} catch (Exception ex) {
System.out.println("Exception in Class::" + className + "::Method::" + methodName + "::" + ex.getMessage());
ex.printStackTrace();

throw new Exception(ex);
}
}

PDFGeneration 工作正常。为 pdfConversion 解析的 HTML 内容具有特殊字符作为适当的实体,如下所示

   StringBuilder content = new StringBuilder();
content.append("<html><body style=\"font-size:12.0pt; font-family:Arial\">
<p>Testes &rarr; &rarr; Vasa efferentia &rarr; Kidney &rarr; Seminal Vescile</p></body></html>");

生成的 pdf 显示“?”相反,使用适当的特殊字符(箭头符号)。 “睾丸??瓦萨传出?肾脏?精囊”。我哪里错了。请指导我。

最佳答案

该解决方案几乎与代码/类/对象无关......

您需要将 CSS“font-family”设置为与您请求的输出字符集相匹配的内容

例如,如果“p”html 标签内有特殊字符,那么您可以使用所需的字体系列设置以下样式:

<HEAD>
<style>
p {
font-family: -good-font-family-
}
</style>
</HEAD>

关于java - 如何使用 iText 和 XMLWorker 在 HTML 到 pdf 转换期间呈现特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com