Java整合FreeMarker匯出Pdf檔案

menghl發表於2024-06-11

引入依賴

<!--Freemarker wls-->
		<dependency>
			<groupId>org.freemarker</groupId>
			<artifactId>freemarker</artifactId>
			<version>2.3.30</version>
		</dependency>
		<dependency>
			<groupId>com.itextpdf.tool</groupId>
			<artifactId>xmlworker</artifactId>
			<version>5.5.11</version>
		</dependency>

		<!-- 支援中文 -->
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>
		<!-- 支援css樣式渲染 -->
		<dependency>
			<groupId>org.xhtmlrenderer</groupId>
			<artifactId>flying-saucer-pdf-itext5</artifactId>
			<version>9.1.18</version>
		</dependency>
    <dependency>
			<groupId>gui.ava</groupId>
			<artifactId>html2image</artifactId>
			<version>2.0.1</version>
		</dependency>

程式碼示例

/**
 * @author alin
 * @date 2024-06-11
 */
@Slf4j
public class TestCreatePdf {

    public static void main(String[] args) throws Exception {
        generatePdfUrl();
    }


    /**
     * 生成pdf
     *
     * @return
     */
    public static String generatePdfUrl() throws Exception {
        // 構造引數
        Model model = assembleData();
        return createPdfAndUpload(beanToMap(model), UUID.randomUUID() + ".pdf", "testCreatePdf.html", "testCreatePdf.css");
    }

    /**
     * 建立pdf並上傳/輸出
     * 
     * @param data
     * @param fileName 檔名
     * @param templateFileName 模板檔名, html模板檔案
     * @param cssPath css檔案路徑
     * @return
     * @throws Exception
     */
    private static String createPdfAndUpload(Map<String, Object> data, String fileName, String templateFileName, String cssPath) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            // 根據模板生成html字串
            String pdf = createHtmlStr(data, templateFileName);
            // 透過html字串生成pdf檔案
            generatePdf(pdf, outputStream, cssPath);
        } catch (Exception e) {
            return null;
        }
        // 輸出/上傳至指定位置
        FileOutputStream out = new FileOutputStream("d:/testPdf/" + fileName);
        out.write(outputStream.toByteArray());
        return "d:/testPdf/" + fileName;
    }

    /**
     * bean轉map
     * 
     * @param bean
     * @return
     */
    public static Map<String, Object> beanToMap(Object bean) {
        Class<?> clazz = bean.getClass();
        return Arrays.stream(clazz.getDeclaredFields())
                .collect(Collectors.toMap(
                        Field::getName,
                        field -> {
                            try {
                                field.setAccessible(true);
                                return field.get(bean);
                            } catch (IllegalAccessException e) {
                                // 處理異常
                                return null;
                            }
                        }
                ));
    }

    /**
     * 模板生成html字串
     *
     * @param data             資料
     * @param templateFileName 模板檔名
     * @throws Exception 捕獲異常
     */
    public static String createHtmlStr(Map<String, Object> data, String templateFileName) throws Exception {
        // 建立一個FreeMarker例項, 負責管理FreeMarker模板的Configuration例項
        Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 指定FreeMarker模板檔案的位置
        cfg.setClassForTemplateLoading(TestCreatePdf.class, "/template");
        // 獲取模板檔案
        Template template = cfg.getTemplate(templateFileName, "UTF-8");
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);
        template.process(data, writer);
        String htmlStr = stringWriter.toString();
        writer.flush();
        writer.close();
        return htmlStr;
    }

    /**
     * 透過html字串生成pdf檔案
     *
     * @param htmlStr
     * @param out
     * @param cssPath
     * @throws IOException
     * @throws DocumentException
     */
    public static void generatePdf(String htmlStr, OutputStream out, String cssPath) throws IOException, DocumentException {
        Document document = new Document(PageSize.A3);
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();
        // html內容解析
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(
                new CssAppliersImpl(new XMLWorkerFontProvider() {
                    @Override
                    public Font getFont(String fontName, String encoding,
                                        float size, final int style) {
                        Font font = null;
                        if (fontName == null) {
                            //字型
                            BaseFont bf;
                            try {
                                bf = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                                font = new Font(bf, size, style);
                            } catch (Exception e) {
                                log.error("getFont", e);
                            }
                        }
                        return font;
                    }
                })) {
            @Override
            public HtmlPipelineContext clone()
                    throws CloneNotSupportedException {
                HtmlPipelineContext context = super.clone();
                try {
                    ImageProvider imageProvider = this.getImageProvider();
                    context.setImageProvider(imageProvider);
                } catch (Exception e) {
                    log.error("clone", e);
                }
                return context;
            }
        };
        // 圖片解析
        htmlContext.setImageProvider(new AbstractImageProvider() {
            @Override
            public String getImageRootPath() {
                return StringUtils.EMPTY;
            }

            @Override
            public Image retrieve(String src) {
                if (StringUtils.isEmpty(src)) {
                    return null;
                }
                try {
                    int pos = src.indexOf("base64,");
                    try {
                        if (src.startsWith("data") && pos > 0) {
                            byte[] img = Base64.decode(src.substring(pos + 7));
                            return Image.getInstance(img);
                        } else if (src.startsWith("http")) {
                            return Image.getInstance(src);
                        }
                    } catch (Exception ex) {
                        log.error("retrieve", ex);
                        return null;
                    }
                    return null;
                } catch (Throwable e) {
                    log.error("retrieve", e);
                }
                return super.retrieve(src);
            }
        });
        htmlContext.setAcceptUnknown(true).autoBookmark(true).setTagFactory(Tags.getHtmlTagProcessorFactory());
        // css解析
        CSSResolver cssResolver = new StyleAttrCSSResolver();
        InputStream cssInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(cssPath);
        CssFile cssfile = XMLWorkerHelper.getCSS(cssInputStream);
        cssResolver.addCss(cssfile);
        HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, writer));
        Pipeline<?> pipeline = new CssResolverPipeline(cssResolver, htmlPipeline);
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser parser = new XMLParser(true, worker, StandardCharsets.UTF_8);
        try (InputStream inputStream = new ByteArrayInputStream(
                htmlStr.getBytes())) {
            parser.parse(inputStream, StandardCharsets.UTF_8);
        }
        document.close();
    }


    /**
     * 組裝資料
     *
     * @return
     * @throws Exception
     */
    private static Model assembleData() throws Exception {
        TestCreatePdf.Model model = new TestCreatePdf.Model();
        model.setCompanyName("公司名稱");
        model.setField1("欄位一");
        model.setField2("欄位二");
        model.setField3("欄位三");
        model.setField4("欄位四");
        model.setField5("欄位五");
        model.setField6("欄位六");
        model.setField7("欄位七");
        model.setRemark("備註~~~~~~~~~");
        model.setSignUrl1("D:/testPdf/test.png");
        model.setSignUrl2("D:/testPdf/test.png");
        model.setSignUrl3("D:/testPdf/test.png");
        model.setSignTime1("2024-04-28 17:08:52");
        model.setSignTime2("2024-04-28 17:08:52");
        model.setSignTime3("2024-04-28 17:08:52");
        List<Object> modeDetailFieldList = Lists.newArrayList();
        modeDetailFieldList.add("表頭一");
        modeDetailFieldList.add("表頭二");
        modeDetailFieldList.add("表頭三");
        modeDetailFieldList.add("表頭四");
        modeDetailFieldList.add("表頭五");
        modeDetailFieldList.add("表頭六");
        model.setModeDetailFieldList(modeDetailFieldList);
        List<List<Object>> modeDetailValueList = Lists.newArrayList();
        for (int i = 1; i < 6; i++) {
            List<Object> valueList = Lists.newArrayList();
            valueList.add("表頭一值--" + i);
            valueList.add("表頭二值--" + i);
            valueList.add("表頭三值--" + i);
            valueList.add("表頭四值--" + i);
            valueList.add("表頭五值--" + i);
            valueList.add("表頭六值--" + i);
            modeDetailValueList.add(valueList);
        }
        model.setModeDetailValueList(modeDetailValueList);
        return model;
    }

    @Data
    public static class Model {

        /**
         * companyName
         */
        private String companyName;

        /**
         * 欄位1
         */
        private String field1;

        /**
         * 欄位2
         */
        private String field2;

        /**
         * 欄位3
         */
        private String field3;

        /**
         * 欄位4
         */
        private String field4;

        /**
         * 欄位5
         */
        private String field5;

        /**
         * 欄位6
         */
        private String field6;

        /**
         * 欄位7
         */
        private String field7;

        /**
         * 備註
         */
        private String remark;

        /**
         * 圖片地址(base64結構)
         */
        private String imgBase64;

        /**
         * signUrl1
         */
        private String signUrl1;

        /**
         * signUrl2
         */
        private String signUrl2;

        /**
         * signUrl3
         */
        private String signUrl3;

        /**
         * signTime1
         */
        private String signTime1;

        /**
         * signTime2
         */
        private String signTime2;

        /**
         * signTime3
         */
        private String signTime3;

        /**
         * 表格欄位名稱
         */
        private List<Object> modeDetailFieldList;

        /**
         * 表格欄位值
         */
        private List<List<Object>> modeDetailValueList;

    }


}

結果

相關文章