itextpdf 版本为 5.5.13
- 传统的方式,把h5渲染到里面后,中文不会显示
PdfPCell cell = new PdfPCell();
cell.setColspan(3);
cell.setRowspan(1);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// h5渲染到cell
String htmlContent = "<h1 style=\"font-size: 36pt;\">Hello, World!</h1><h2>我的</h2><p style=\"color: red;\">This is some HTML content.</p><img src=\"http://49.232.167.247:22038/vehicle-quality-review-oss/2024/04/11/1712827970222_20240411173255A009.jpg\" />";
ElementList elementList = XMLWorkerHelper.parseToElementList(htmlContent, css);
for (Element element :elementList) {
cell.addElement(element);
}
table.add(cell);
- 解决方案:重写
XMLWorkerHelper的parseToElementList这个方法
PdfPCell cell = new PdfPCell();
cell.setColspan(3);
cell.setRowspan(1);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// h5渲染到cell
String htmlContent = "<h1 style=\"font-size: 36pt;\">Hello, World!</h1><h2>我的</h2><p style=\"color: red;\">This is some HTML content.</p><img src=\"http://49.232.167.247:22038/vehicle-quality-review-oss/2024/04/11/1712827970222_20240411173255A009.jpg\" />";
ElementList elementList = parseToElementList(htmlContent, null);
cell.addElement(new Paragraph().addAll(elementList));
table.addCell(cell);
/**
* html转pdf 写法
* @param html
* @param css
* @return
* @throws IOException
*/
public static ElementList parseToElementList(String html, String css) throws IOException {
// CSS
CSSResolver cssResolver = new StyleAttrCSSResolver();
if (css != null) {
CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(css.getBytes()));
cssResolver.addCss(cssFile);
}
// HTML
MyFontsProvider fontProvider = new MyFontsProvider();
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.autoBookmark(false);
// Pipelines
ElementList elements = new ElementList();
ElementHandlerPipeline end = new ElementHandlerPipeline(elements, null);
HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end);
CssResolverPipeline cssPipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
// XML Worker
XMLWorker worker = new XMLWorker(cssPipeline, true);
XMLParser p = new XMLParser(worker);
html = html.replace("<br>", "").replace("<hr>", "").replace("<img>", "").replace("<param>", "").replace("<link>", "");
p.parse(new ByteArrayInputStream(html.getBytes()));
return elements;
}
static class MyFontsProvider extends XMLWorkerFontProvider {
public MyFontsProvider() {
super(null, null);
}
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) {
return getFontInf();
}
}
/**
* 设置字体信息
* @return
*/
@SneakyThrows
private static Font getFontInf() {
// 字体路径
String fontPath = "/fonts/STSong.TTF";
BaseFont baseFont = null;
Font font = null;
// 设置字体路径,字体编码,是否将字体嵌入pdf(默认false)
baseFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 设置默认字体数据
font = new Font(baseFont, 12f, Font.NORMAL,BaseColor.BLACK);
return font;
}
409



被折叠的 条评论
为什么被折叠?



