写在前面

以下路径问题根据项目结构自己修改,以下是我使用spring boot打成jar包的写法。

一、需求背景

在前端编辑器中输入任意的文本,包括css样式变化,保存为html文本。通过Java后台将html文本转换为PDF文档并加上页眉、页脚、水印等。因为网上开源的方案用的工具版本都比较老,也无法满足要求。所以只能用目前比较新的Itext7,网上的资料不多,只能看文档自己学习。

二、解决方案

1.开发工具Itext7 (https://itextpdf.com/itext7): 首先jar包一定要引对,要不Demo也运行不了。我项目使用的是maven,以下是pom.xml最新版jar可以通过官方文档中寻找。

<!-- pdfHTML -->
<dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>html2pdf</artifactId>
 <version>1.0.2</version>
</dependency>
<!-- add all iText 7 Community modules -->
<dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itext7-core</artifactId>
 <version>7.0.5</version>
 <type>pom</type>
</dependency>

itext7-core包含了9个jar包,直接都导入就好

2.最简单的HTML转PDF(包含中文字体、粗体、表格等基本,不支持PDF的页眉、页脚、页边距、水印等)方法的参数和返回值可以灵活变通

public class Html2PdfUtil {
    public static void main(String[] args) throws Exception {
        String html = "<p><span style=\"font-family: Microsoft YaHei;\">微软雅黑: 粗体前A<strong>A粗体A</strong>A粗体后</span></p>\n" +
                "<p><span style=\"font-family: SimSun;\">宋体: 粗体前A<strong>A粗体A</strong>A粗体后</span></p>\n" +
                "<p><span style=\"font-family: STHeiti;\">黑体: 粗体前A<strong>A粗体A</strong>A粗体后</span></p>" +
                "<p><span style=\"font-family: Times New Roman;\">Times New Roman: pre bdA<strong>AbdA</strong>Aaft bd</span></p>\n";
        FileOutputStream fileOutputStream = new FileOutputStream("D:/Test/a.pdf");
        fileOutputStream.write(convert(html));
        fileOutputStream.close();
    }
    public static byte[] convert(String html) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ConverterProperties props = new ConverterProperties();
        FontProvider fp = new FontProvider(); // 提供解析用的字体
        fp.addStandardPdfFonts(); // 添加标准字体库、无中文
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        fp.addDirectory(classLoader.getResource("fonts").getPath()); // 自定义字体路径、解决中文,可先用绝对路径测试。
        props.setFontProvider(fp);
        // props.setBaseUri(baseResource); // 设置html资源的相对路径
        HtmlConverter.convertToPdf(html, outputStream, props); // 无法灵活设置页边距等
        byte[] result = outputStream.toByteArray();
        outputStream.close();
        return result;
    }
}

这段代码添加了中文字体库,否则中文无法显示。设置了解析html时资源的相对路径。还有就是如果html编辑器有加粗样式类的需求时,需要把该字体和加粗字体都上传,否则无法正常显示,字体不太好找,大部分字体系统盘中有,附宋体加粗字体,和华文黑体加粗,黑体加粗没找到。

https://files.cnblogs.com/files/Sigurd/STHeitibd.rar
https://files.cnblogs.com/files/Sigurd/simsunbd.rar

3.如何灵活设置生成pdf的页边距

首先要找到在哪里设置页边距,找了一圈发现只有Document类中有这个方法,所有刚开始我用了HtmlConvert.convertToDocument()方法,但是发现得到的Document类immediateFlush为true,没办法手动重新布局,只能换方法了。

然后看到了List

IBlockElement中没有修改边距的方法,看一下IBlockElement的实现类BlockElement中可以修改边距,然后强转成BlockElement就可以修改行句了。最后代码如下。

public class Html2PdfUtil {
    public static final float topMargin = 114f;
    public static final float bottomMargin = 156f;
    public static final float leftMargin = 90f;
    public static final float rightMargin = 90f;
    public static byte[] convert(String html) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PdfWriter writer = new PdfWriter(outputStream);
        PdfDocument pdfDocument = new PdfDocument(writer);
        try {
            ConverterProperties props = new ConverterProperties();
            FontProvider fp = new FontProvider();
            fp.addStandardPdfFonts();
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            fp.addDirectory(classLoader.getResource("fonts").getPath());
            props.setFontProvider(fp);
            List<IElement> iElements = HtmlConverter.convertToElements(html, props);
            Document document = new Document(pdfDocument, PageSize.A4, true); // immediateFlush设置true和false都可以,false 可以使用 relayout
            document.setMargins(topMargin, rightMargin, bottomMargin, leftMargin);
            for (IElement iElement : iElements) {
                BlockElement blockElement = (BlockElement) iElement;
                blockElement.setMargins(1, 0, 1, 0);
                document.add(blockElement);
            }
            document.close();
            return outputStream.toByteArray();
        } catch (Exception e) {
            throw e;
        } finally {
            outputStream.close();
        }
    }
}

4.页眉页脚水印的写法

页眉页脚水印都是在每页插入相同内容,所以做法类似。都是实现IEventHandler接口,然后添加监听。页眉页脚水印、基本都是图片和文字调用的api也比较简单,原点坐标在左下角,在第一象限内做图,以下面的代码为例吧。

/**
 * Description html转pdf
 * Created by shuxiaogang 
 * date on 2017/11/22
 */
public class Html2PdfUtil {
    public static final float topMargin = 114f;
    public static final float bottomMargin = 156f;
    public static final float leftMargin = 90f;
    public static final float rightMargin = 90f;

    public static byte[] convert(String html) throws IOException {
        ...同代码3
        Header headerHandler = new Header();
        Footer footerHandler = new Footer();
        WatermarkingEventHandler watermarkingEventHandler = new WatermarkingEventHandler();
        pdfDocument.addEventHandler(PdfDocumentEvent.START_PAGE, headerHandler);
        pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, footerHandler);
        pdfDocument.addEventHandler(PdfDocumentEvent.INSERT_PAGE, watermarkingEventHandler);
        ...同代码3
    }
    // 页眉
    protected static class Header implements IEventHandler {
        protected float width = 102f;
        protected float height = 32f;
        protected float x = 42f;
        protected float y = 740f;
        @Override
        public void handleEvent(Event event) {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
            PdfDocument pdf = docEvent.getDocument();
            PdfPage page = docEvent.getPage();
            Rectangle pageSize = page.getPageSize();
            PdfCanvas pdfCanvas = new PdfCanvas(
                    page.getLastContentStream(), page.getResources(), pdf);
            Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
            ImageData image = null;
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            InputStream logo = loader.getResourceAsStream("imgaes/logo.jpg");
            try {
                image = ImageDataFactory.create(toByteArray(logo));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image img = new Image(image);
            img.scaleAbsolute(width, height); // 图片宽高
            img.setFixedPosition(x, y); // 图片坐标 左下角(0,0)
            canvas.add(img);
        }
    }
    // 页脚
    protected static class Footer implements IEventHandler {
        protected PdfFormXObject placeholder; // 相对坐标系
        protected float x = 82f;
        protected float y = 50f;
        protected float imageWidth = 6f;
        protected float imageHeight = 78f;
        protected float space = 10f;
        public Footer() {
            placeholder =
                    new PdfFormXObject(new Rectangle(0, 0, 500, 78));
        }
        @Override
        public void handleEvent(Event event) {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
            PdfDocument pdf = docEvent.getDocument();
            PdfPage page = docEvent.getPage();
            Rectangle pageSize = page.getPageSize();
            PdfCanvas pdfCanvas = new PdfCanvas(
                    page.getLastContentStream(), page.getResources(), pdf);
            pdfCanvas.addXObject(placeholder, x + space, y);
            Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
            ImageData image = null;
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            InputStream buleRed = loader.getResourceAsStream("imgaes/bule_red.JPG");
            try {
                image = ImageDataFactory.create(toByteArray(buleRed));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Image img = new Image(image);
            img.scaleAbsolute(imageWidth, imageHeight);
            img.setFixedPosition(x, y);
            canvas.add(img);
            writeInfo(pdf);
            pdfCanvas.release();
        }
        public void writeInfo(PdfDocument pdf) {
            Canvas canvas = new Canvas(placeholder, pdf);
            canvas.setFontSize(7.5f);
            PdfFont pdfFont = null;
            try {
                // 微软雅黑
                ClassLoader loader = Thread.currentThread().getContextClassLoader();
                InputStream msyh = loader.getResourceAsStream("fonts/msyh.ttf");
                pdfFont = PdfFontFactory.createFont(toByteArray(msyh), PdfEncodings.IDENTITY_H, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
            canvas.setFont(pdfFont); // 需要单独设置一下字体才能使用中文
            canvas.showTextAligned("http://www.xxxx.com",
                    0, 65, TextAlignment.LEFT);
            canvas.showTextAligned("深圳市南山区学府路东xxxxx  xxxxxx",
                    0, 50, TextAlignment.LEFT);
            canvas.showTextAligned("xxxxx Ixxxxxx,Xuefu Road Ease,Nan Shan District, Shenzhen xxxxxx",
                    0, 35, TextAlignment.LEFT);
            canvas.showTextAligned("Tel:0755-xxxxx Fax:212-xxxxxx",
                    0, 20, TextAlignment.LEFT);
        }
    }
    // 水印
    protected static class WatermarkingEventHandler implements IEventHandler {
        protected float x = 298f;
        protected float y = 421f;
        @Override
        public void handleEvent(Event event) {
            PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
            PdfDocument pdfDoc = docEvent.getDocument();
            PdfPage page = docEvent.getPage();
            PdfFont font = null;
            try {
                font = PdfFontFactory.createFont(FontConstants.HELVETICA_BOLD);
            } catch (IOException e) {
                e.printStackTrace();
            }
            PdfCanvas canvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
            new Canvas(canvas, pdfDoc, page.getPageSize())
                    .setFontColor(Color.LIGHT_GRAY)
                    .setFontSize(60)
                    .setFont(font)
                    .showTextAligned(new Paragraph("W A T E R M A R K"), x, y, pdfDoc.getPageNumber(page),
                            TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
        }
    }

    public static byte[] toByteArray(InputStream input) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] buffer = new byte[4096];
        int n = 0;
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, n);
        }
        return output.toByteArray();
    }
}