zoukankan      html  css  js  c++  java
  • ITextRenderer转PDF

    //向模板里填数据
        public void createHtml (User user,Long waybillId) throws Exception{
            //创建一个合适的Configration对象
            Configuration configuration = new Configuration();
            //指定模板路径
            configuration.setDirectoryForTemplateLoading(new File(TEMPLATE_PATH));
            configuration.setObjectWrapper(new DefaultObjectWrapper());
            configuration.setDefaultEncoding("UTF-8");
            //获取模版
            Template template = configuration.getTemplate(TEMPLATE);
    
            Map<String, Object> paramMap = new HashMap<String, Object>();
            WaybillVO waybillVO = waybillManageService.getWaybillInfo(user, waybillId);
            paramMap.put("id", waybillVO.getWaybill().getId().toString());
            paramMap.put("customerOrderNumber", waybillVO.getWaybill().getCustomerOrderNumber() == null ? "" : waybillVO.getWaybill().getCustomerOrderNumber());
            paramMap.put("companyName", waybillVO.getShipperCompany().getCompanyName());
            paramMap.put("consigneePhone", waybillVO.getWaybill().getConsigneePhone());
            paramMap.put("expectedDeliveryTime",
                    new SimpleDateFormat("yyyy-MM-dd").format(waybillVO.getWaybill().getExpectedDeliveryTime()));
            paramMap.put("waitUnloading", waybillVO.getWaybill().getWaitUnloading());
    
            paramMap.put("consigneeName", waybillVO.getWaybill().getConsigneeName());
            paramMap.put("toDetail", waybillVO.getWaybill().getToDetail());
            int colspanLength = 2;
            if (waybillVO.getVehicleVOList() != null) {
                colspanLength += waybillVO.getVehicleVOList().size();
            }
            paramMap.put("colspanLength", colspanLength);
            paramMap.put("thirdName", waybillVO.getVarietyMap().get("thirdName").toString());
            paramMap.put("amount", waybillVO.getCargo().getAmount().toString());
            paramMap.put("specifications", waybillVO.getCargo().getSpecifications());
    
            String weight = "";
            if (waybillVO.getCargo().getWeight() != null) {
                weight = waybillVO.getCargo().getWeight().toString();
            } else if (waybillVO.getCargo().getVolume() != null) {
                weight = waybillVO.getCargo().getVolume().toString();
            }
            paramMap.put("weight",weight);
    
            paramMap.put("packingManner",waybillVO.getCargo().getPackingManner());
            paramMap.put("expectedPickupTime", new SimpleDateFormat("yyyy-MM-dd").format(waybillVO.getWaybill().getExpectedPickupTime()));
            paramMap.put("fromDetail", waybillVO.getWaybill().getFromDetail());
            paramMap.put("fromCompany", waybillVO.getWaybill().getFromCompany());
            paramMap.put("consignerName", waybillVO.getWaybill().getConsignerName());
            paramMap.put("consignerPhone", waybillVO.getWaybill().getConsignerPhone());
            paramMap.put("remark", waybillVO.getWaybill().getRemark() == null ? "" : waybillVO.getWaybill().getRemark());
            paramMap.put("vehicleVOList", waybillVO.getVehicleVOList());
            paramMap.put("staffRealName", waybillVO.getStaffAccount().getRealName());
            paramMap.put("printTime", new SimpleDateFormat("yyyy-MM-dd HH:MM:ss").format(waybillVO.getPrintTime()));
    
            Writer writer  = new OutputStreamWriter(new FileOutputStream(TRANS),"UTF-8");
            template.process(paramMap, writer);
        }
    public void iTextRendererToPDF (User user,Long waybillId) throws Exception{
            //向模板里填数据
            createHtml(user,waybillId);
    
            // 将HTML转PDF
            String url = new File(TRANS).toURI().toURL().toString();
            OutputStream os = new FileOutputStream(DEST);
            ITextRenderer renderer = new ITextRenderer();
            //解决中文支持问题
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    
            renderer.setDocument(url);
    
            renderer.layout();
            renderer.createPDF(os);
            renderer.finishPDF();
            os.close();
        }
  • 相关阅读:
    app令牌的一个token实现
    velocity分页模板
    js基础-表单验证和提交
    做项目中没经验遇到的各种问题
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    oracle创建用户
    oracle创建表相关
    pe创建激活administrator后消除问题,删除用户问题
    spring学习遇到的问题汇总
    .NET MVC自定义错误处理页面的方法
  • 原文地址:https://www.cnblogs.com/xiaoSY-learning/p/5817268.html
Copyright © 2011-2022 走看看