填充html以生成pdf openhtmltopdf



我想创建一个表格pdf,所以我使用openhtmltopdf,我想将我的列表数据从java传递到html,然后渲染到pdf。我该怎么做?

private static Document html5ParseDocument(String inputHTML) throws IOException {
org.jsoup.nodes.Document doc;
System.out.println("parsing ...");
doc = Jsoup.parse(new File(inputHTML), "UTF-8");
System.out.println("parsing done ..." + doc);
return new W3CDom().fromJsoup(doc);
}
private static void htmlToPdf(String inputHTML, String outputPdf) throws IOException {
Document doc = html5ParseDocument(inputHTML);

OutputStream os = new FileOutputStream(outputPdf);
PdfRendererBuilder builder = new PdfRendererBuilder();
builder.withUri(outputPdf);
builder.toStream(os);

builder.withW3cDocument(doc, System.clearProperty("user.dir").concat("/src/main/resources"));
//builder.useUriResolver(new MyResolver());
builder.run();
System.out.println("PDF generation completed");
os.close();
}

您可以使用模板,比如说在html中,您需要更改标题;

在html 中

<title>
#{header}
</title>

在您的java代码中,您可以将#{header}替换为headerString;

inputHtml.replace("#{header}","New title");

由于您的问题是关于列表的,所以您需要将唯一的html标签替换为您在java代码中构建的html字符串。

最新更新