ITextRenderer 生成PDF文件 背景有白边

使用ITextRenderer将HTML转为PDF格式文件 (JFinal框架)

Java 代码如下

public static void main7(String[] ages) throws Exception {
        html2pdf("E:\\IDEA\\he\\src\\main\\webapp\\pdf\\test.html", "E:\\IDEA\\tianlan\\target\\c.pdf");
    }
    public static void html2pdf(String inputFile, String outFile) throws Exception {
        String url;
        OutputStream os = null;
        try {
            url = new File(inputFile).toURI().toURL().toString();
            os = new FileOutputStream(outFile);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(url);

            ITextFontResolver fontResolver = renderer.getFontResolver();       
            fontResolver.addFont("E:/IDEA/tianlan/src/main/webapp/pdf/font/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            fontResolver.addFont("E:/IDEA/he/src/main/webapp/pdf/font/fontawesome-webfont.ttf", com.itextpdf.text.pdf.BaseFont.IDENTITY_H, com.itextpdf.text.pdf.BaseFont.NOT_EMBEDDED);
            //生成pdf界面布局
            renderer.layout();
            //开始创建PDF并往目标文件导出pdf数据
            renderer.createPDF(os);
            //完成创建,自动关闭Document资源
            renderer.finishPDF();
        } catch (MalformedURLException e) {
            e.printStackTrace();
            throw new Exception("生成pdf失败");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new Exception("生成pdf失败");
        } catch (com.lowagie.text.DocumentException e) {
            e.printStackTrace();
            throw new Exception("生成pdf失败");
        } catch (IOException e) {
            e.printStackTrace();
            throw new Exception("生成pdf失败");
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new Exception("生成pdf失败");
                }
            }
        }
    }

test.html 代码如下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>test</title>
</head>
<body style="background-color: red">
</body>
</html>

c.pdf截图

生成的PDF会有一个白色的边,感觉有边距。

我以为   renderer.createPDF(os);  会将边距默认为 0 ,但是还是会有白边

尝试使用以下代码,可以去掉边距,但是很多HTML样式没有办法使用,要形成的PDF格式要求比较多,所以放弃了

Document document = new Document();
document.setMargins(0,0,0,0);
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();

new ByteArrayInputStream(content.getBytes());
XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(content.getBytes()), null, StandardCharsets.UTF_8, fontImp);
(这段代码不是完整的)

请问:这个边距是怎么产生的,有没有办法在使用上述代码   ITextRenderer  的基础上去掉白边。有其他更好的建议也可以

谢谢

通过修改html样式可以去掉边距,在html中定义样式,使用"@page"可以实现。

@page:left{
   margin: 0cm;
} 
@page:right{
   margin: 0cm;
}
  1. @page:用于修改打印时样式,不会改变页面在浏览器上面的样式,只会影响打印时的样式
  2. @page:left:页面左边样式
  3. @page:right:页面右边样式
  4. 将margin设置为0即可消除边距。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>test</title>
    <style>
        @page {

        }
        @page:left{
            margin: 0cm;
        } 
        @page:right{
            margin: 0cm;
        }
    </style>
</head>
<body style="background-color: red">
</body>
</html>

这是我生成的pdf:

 

<style type="text/css">
    @page{size:a4}
</style>

给html加上样式:设置为A4纸的大小。