我试图使用 iText 和 Java 将 HTML 转换为 PDF 文件。但是,由于标签 <body> 背景属性,我的 PDF 文件有一个颜色不同的区域。 PDF 文件应具有与 <div class='container'> 类相同的全白背景。有没有什么方法可以实现它而不是将 <body> 背景更改为白色?
这是我的 HTML 和 CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
@page {
margin-left: 0;
margin-right: 0;
margin-top: 0;
@bottom-left {
margin-left: 10px;
content: "Page " counter(page) " / " counter(pages);
}
}
body {
scroll-behavior: smooth;
color: #000;
font-weight: normal;
background: #f0f0f0;
background-color: rgb(244, 180, 103);
width: 100%;
line-height: 20pt;
text-align: justify;
margin: 0 auto;
height: 100%;
}
@media print {
body {
background-color: #fff;
}
}
.container {
scroll-behavior: smooth;
width: 210mm;
padding: 25mm 25mm 25mm 35mm;
background: #FFF;
margin: 0 auto;
box-sizing: border-box;
position: relative;
font-size: 14pt;
height: 100%;
}
</style>
</head>
<body>
<div class="container">
<h1>CONTAINER</h1>
<p>This is my data</p>
<p>This is my data</p>
<p>This is my data</p>
<p>This is my data</p>
<p>This is my data</p>
<p>This is my data</p>
<p>This is my data</p>
<p>This is my data</p>
</div>
</body>
</html>
这是我的Java代码
private static final String HTML_FILE_PATH = "C:/yourlocation/input.html";
private static final String PDF_FILE_PATH = "C:/yourlocation/output.pdf";
public static void main(String[] args) throws IOException {
System.out.println("Hello World!");
FontProvider fontProvider = new DefaultFontProvider(true, true, true);
ConverterProperties properties = new ConverterProperties();
properties.setCharset("UTF-8");
properties.setFontProvider(fontProvider);
File htmlFile = new File(HTML_FILE_PATH);
File pdfFile = new File(PDF_FILE_PATH);
try (FileInputStream fStream = new FileInputStream(htmlFile)) {
System.out.println("BEGIN to convert HTML to PDF");
HtmlConverter.convertToPdf(fStream, new FileOutputStream(pdfFile), properties);
System.out.println("FINISHED convert HTML to PDF");
} catch (IOException e) {
e.printStackTrace();
}
}
好的,所以我可以使用 iText 通过扩展 BodyTagCssApplier 更改 <body> 标签的背景颜色,覆盖,复制整个 apply 方法并将其背景颜色(在下面的 cssProps 中)设置为白色。
private static class CustomBodyTagCssApplier extends BodyTagCssApplier {
@Override
public void apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker) {
Map<String, String> cssProps = stylesContainer.getStyles();
// Set background-color to white
cssProps.put(CommonCssConstants.BACKGROUND_COLOR, "#fff");
BodyHtmlStylesContainer styleProperty = new BodyHtmlStylesContainer();
IPropertyContainer container = tagWorker.getElementResult();
if (container != null) {
BackgroundApplierUtil.applyBackground(cssProps, context, styleProperty);
MarginApplierUtil.applyMargins(cssProps, context, styleProperty);
PaddingApplierUtil.applyPaddings(cssProps, context, styleProperty);
BorderStyleApplierUtil.applyBorders(cssProps, context, styleProperty);
if (styleProperty.hasStylesToApply()) {
container.setProperty(Html2PdfProperty.BODY_STYLING, styleProperty);
}
FontStyleApplierUtil.applyFontStyles(cssProps, context, stylesContainer, container);
}
}
}
然后使用 ConverterProperties 将该 CSS 应用于我的 <body> 标签
public static void main(String[] args) {
// Some code in question above
ConverterProperties properties = new ConverterProperties();
properties.setCssApplierFactory(new DefaultCssApplierFactory() {
@Override
public ICssApplier getCustomCssApplier(IElementNode tag) {
if ("body".equals(tag.name())) {
return new CustomBodyTagCssApplier();
}
return null;
}
});
// Some code in question above
// HtmlConverter.convertToPdf(fStream, new FileOutputStream(pdfFile), properties);
}