使用Jacob来进行word转pdf或其他格式

之前因为业务需要,需要将生成的word文件转换为pdf格式,通过网上整理,总结了几种方法。

1.使用jacob,(完美转换,不过不支持linux服务器),刚开始是用它做的很方便,但经理说要在linux上,不能使用dll文件(使用Jacob需要在systme32里面添加dll文件)。

2.使用openoffice,(需要先转为html,或者模板设计为html)123M的软件,支持各种服务器。

3.使用aspose(收费)

由于我们要使用linux服务器,所以采用了aspose方法,另外还要itext和flying不过不够完善。

private Map<String, String> createOnlinePlan(TmPlan plan) {
        Map<String, String> resultMap = new HashMap<>();
        String fname = UUID.randomUUID().toString();

        try {
            Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
            cfg.setDefaultEncoding("UTF-8");
            HashMap<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("trademark_name", plan.getTm_word());
            dataMap.put("telephone", plan.getMobile() == null ? "未提供" : plan.getMobile());
            dataMap.put("email", plan.getEmail() == null ? "未提供" : plan.getEmail());
            dataMap.put("plan_code", "20160700000012");
            dataMap.put("plan_gen_date", DateUtil.getNow("yyyy-MM-dd"));
            Map<String, List<PlanDocRecord>> docMap = convertPlanDocMap(plan);
            dataMap.put("madeli", docMap.get("madeli"));
            dataMap.put("single", docMap.get("single"));
            String imageBase64 = "";
            if (plan.getTm_file() != null && !plan.getTm_file().trim().equals("")) {
                String webroot = ApplicationContextHolder.getContext().getServletContext().getRealPath(ApplicationContextHolder.getContext().get("webroot_image_relative"));
                if (new File(webroot + File.separator + plan.getTm_file()).exists()) {
                    imageBase64 = CollectUtil.getImageStr(webroot + File.separator + plan.getTm_file());
                    if (imageBase64 == null) {
                        imageBase64 = CollectUtil.getImageStr(ApplicationContextHolder.getContext().getServletContext().getRealPath("/") + "tm_image_default.gif");
                    }
                } else {
                    imageBase64 = CollectUtil.getImageStr(ApplicationContextHolder.getContext().getServletContext().getRealPath("/") + "tm_image_default.gif");
                }
            }
            dataMap.put("tm_image", imageBase64);

            logger.error(ApplicationContextHolder.getContext().getServletContext().getRealPath("/") + "WEB-INF/ftl_template");
            cfg.setDirectoryForTemplateLoading(new File(ApplicationContextHolder.getContext().getServletContext().getRealPath("/") + "WEB-INF/ftl_template"));
            Template template = cfg.getTemplate("plan.ftl");
            String plan_root = ApplicationContextHolder.getContext().get("plan_root");
            // File outFile = new
            // File("E://unitalen_workspace//unitalen_nginx//WebContent//download//"
            // + fname + ".doc");
            File outFile = new File(plan_root + fname + ".doc");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));
            template.process(dataMap, out);
            out.close();

            com.aspose.words.Document conversiondoc = new com.aspose.words.Document(plan_root + fname + ".doc");
            conversiondoc.save(plan_root + fname + ".pdf", SaveFormat.PDF);
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        resultMap.put("fpath", fname + ".pdf");
        return resultMap;
    }
原文地址:https://www.cnblogs.com/zixiaopiaomiao/p/5756944.html