freemark xml 生成doc、aspose 生成pdf

1)生成doc

    采用模板生成doc,优点:兼容性强、样式丰富且可控。

     模板制作流程:

  1. 准备好doc模板,对于需要动态填充的内容,用标签符替代,如: 
  2. 将doc另存为xml格式,如果模板中存在需根据内容动态填充的图片信息时,可先插入一张固定图片,然后再xml中找到<pkg:binaryData>标签,将标签中的base64字符串替换为标签符,如:
  3. 将xml文件,另存为ftl格式,该文件为程序中需要用到的模板文件

     生成流程:

  1. 引入依赖:
    implementation group: 'org.freemarker', name: 'freemarker', version: '2.3.28'
  2. 核心代码:
public static void writerWordFile(String savePath) throws IOException {
    Configuration configuration =  new Configuration();
    configuration.setDefaultEncoding("utf-8");
    configuration.setDirectoryForTemplateLoading(new File("F:/"));
    Map<String, Template> allTemplates = new HashMap<>();
    try {
        allTemplates.put("resume", configuration.getTemplate("安全生产行政执法文书_v3.ftl"));
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
    File f = new File(savePath);
    Template t = allTemplates.get("resume");
    try {
        String selected = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAyklEQVQ4T9VT0QlCMQzs5S3gJoZmAZ1AN9ARnhPoCLqBm6gDtHQE3cAFXiIRBFG0PBHB/PbuyN2lEJFpCGEZQuDQY8zsCGCBGONRVeellH0PfmDmERGtISKWUkIf8g3r3D8SYOZryKWU0tuCk4lop6rjqkCMsQUw67rOwWdmHjRNszOzTc55ex/2yxBFxIFDFyEir3efc24fm3rbgouY2QTAIaXkx/Y01Rrdjqpu3cpHArXjqm7wG4FvfKapma0ADGsr37+b2QlAewH+QJnPSgMBHQAAAABJRU5ErkJggg==";
        String notSelected = "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAATklEQVQ4T2NkgIACBgYGASibWOoDAwPDBEao5gMMDAwXiNUJVWfAwMDgADKgAYpJ1A9W3jBqwGgYDKN0AMqJFGUm5Oz8n8gcBcpD4OwMAHiZG/HuHKfxAAAAAElFTkSuQmCC";
        Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("a","11111");
        dataMap.put("b","222");
        dataMap.put("c","33333333333333333333333333333");
        dataMap.put("d","444444444444444444444444444444");
        dataMap.put("e","555");
        dataMap.put("f","666");
        dataMap.put("img1",notSelected);
        dataMap.put("img2",notSelected);
        dataMap.put("img3",notSelected);
        dataMap.put("img4",selected);
        t.process(dataMap, w);
        w.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new RuntimeException(ex);
    }

2)根据doc生成pdf

    本案例采用aspose破解版,仅供学习参考。

    生成流程:

  1. 下载破解lib:aspose-words-18.6-jdk16.jar、JByteMod-1.8.0.jar

https://download.csdn.net/download/tomosun/12254972

  1. 核心代码:
public static void docToPDF(String inPath, String outPath) throws Exception {
    if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
        return;
    }
    try {
        long old = System.currentTimeMillis();
        File file = new File(outPath); // 新建一个空白pdf文档
        FileOutputStream os = new FileOutputStream(file);
        // Address是将要被转化的word文档
        Document doc = new Document(inPath);
        doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF,
        // EPUB, XPS, SWF 相互转换
        long now = System.currentTimeMillis();
        System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static boolean getLicense() throws Exception {
    boolean result = false;
    try {

        InputStream is = com.aspose.words.Document.class
                .getResourceAsStream("/com.aspose.words.lic_2999.xml");
        License aposeLic = new License();
        aposeLic.setLicense(is);
        result = true;
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return result;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值