使用itext将html转pdf怎么同时显示中文或图片?

用ITextRenderer生成pdf不显示中文,用XMLWorkerHelper.getInstance().parseXHtml则不显示图片, 有没有能同时解决中文和图片的方法,图片是用的base64

content是html字符串 dest是输出的pdf

ITextRenderer
try(FileOutputStream fileOutputStream = new FileOutputStream(dest)) {

ITextRenderer renderer = new ITextRenderer();

ITextFontResolver fontResolver = renderer.getFontResolver();

fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

renderer.setDocumentFromString(content);

renderer.layout();

renderer.createPDF(fileOutputStream);

} catch (DocumentException e) {

e.printStackTrace();

}

用XMLWorkerHelper
Document document = new Document();

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));

document.open();

XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);

fontImp.register(FONT);

byte[] b = content.getBytes(StandardCharsets.UTF_8);

ByteArrayInputStream bais = new ByteArrayInputStream(b);

XMLWorkerHelper.getInstance().parseXHtml(writer, document, bais, StandardCharsets.UTF_8);

bais.close();

document.close();

html"><div class="signature-new">

<p>${fileType}</p>

<img src="data:${fileType};base64,${file64Str}"

width="100px"

height="100px"

alt=""/>

<p style="font-family: simsun,serif">一二三</p>

</div>


回答:

itext对css的font大小写敏感,字体应该是SimSun 不是simsun

以上是 使用itext将html转pdf怎么同时显示中文或图片? 的全部内容, 来源链接: utcz.com/p/944942.html

回到顶部