• 使用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;
        }
  • 相关阅读:
    Android的webview的设置参数
    禁止RecycleView滑动
    Volley加载不出图片的问题
    LIstview滑动时不加载图片,停止时加载!
    【原创】设计模式开篇—面向对象的软件设计
    [原创]写给自己的总结—2014到2015
    【原创】开车分四个阶段的话,你属于哪个
    【原创】亲身经历的几次合同陷阱
    【转】程序员需谨记的8条团队开发原则
    【转】绩效考核的10大误区
  • 原文地址:https://www.cnblogs.com/zixiaopiaomiao/p/5756944.html
Copyright © 2020-2023  润新知