且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当HTML作为输入字符串和CSS作为文件传递时,使用Html2pdf将Itext7 HTML转换为PDF

更新时间:2023-02-08 22:09:17

您不再需要显式传递/添加CSS文件,pdfHTML(Html2Pdf是一段时间内的内部开发名称),可以选择链接到样式表中的样式表. HTML.如果您的CSS与HTML不在同一个文件夹中,则可能必须定义一个基本资源位置.

You no longer have to pass/add the CSS file explicitly, pdfHTML(Html2Pdf was the internal development name for a while), can pick up stylesheets linked in the HTML. You might optionally have to define a base resource location if your CSS is not located in the same folder as your HTML.

在您的HTML中,在标题中添加以下行

In your HTML, add the following line in the header

<link rel="stylesheet" type="text/css" href="bootstrap.css"/>

您想要将其添加到

File pdf = new File(pdfDest);
pdf.getParentFile().mkdirs();

PdfWriter writer  = new PdfWriter(pdfDest);
PdfDocument pdfDoc = new PdfDocument(writer);

ConverterProperties converterProperties = new ConverterProperties().setBaseUri(resourceLoc);

HtmlConverter.convertToPdf(new FileInputStream(htmlSource), pdfDoc, converterProperties);
pdfDoc.close();