我正在从 html 构建 pdf,我想设置表格边框。 这是我的 html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Digital </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body style="font-size:12.0pt; font-family:Arial">
<table style="width:100%" border=\"2\">
<tr>
<th>depo date</th>
<th>MMR</th>
<th>SUM</th>
<th>NUM</th>
<th>ACCOUNT</th>
<th>TNUM</th>
</tr>
<tr>
<td dir="rtl">$data.depo</td>
<td dir="rtl">$data.MR</td>
<td dir="rtl">$data.Sum</td>
<td dir="rtl">$data.Num</td>
<td dir="rtl">$data.accoun</td>
<td dir="rtl">$data.branch</td>
<td dir="rtl">$data.TNum</td>
</tr>
</table>
</body>
</html>
已关注 iText-add-table-to-pdf ,唯一的区别是在表格中添加了 border=\"2\"
,但它仍然不起作用,我还需要添加更多更改吗?
创建pdf功能(与教程中的相同):
public void createPdf(String file, String htmlString, String pathToTTF) throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream(file));
// step 3
document.open();
// step 4
// Styles
CSSResolver cssResolver = new StyleAttrCSSResolver();
XMLWorkerFontProvider fontProvider =
new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS); //
fontProvider.register(pathToTTF);
CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(cssAppliers);
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, writer);
HtmlPipeline html = new HtmlPipeline(htmlContext, pdf);
CssResolverPipeline css = new CssResolverPipeline(cssResolver, html);
// XML Worker
XMLWorker worker = new XMLWorker(css, true);
XMLParser p = new XMLParser(worker);
p.parse(new FileInputStream(htmlString), Charset.forName("UTF-8"));
// step 5
document.close();
}
请您参考如下方法:
将 html 转换为 pdf 时,请考虑使用 pdfHtml。
pdfHtml 是我们插件部分中执行此任务的最新工具,并且在 CSS 和 HTML 构造方面具有更大的范围。