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();
        }
  • 相关阅读:
    CPP流类库与输入输出
    STL学习之mismatch();
    谷歌浏览器现在点击任何文本都会出现光标
    jQuery删除元素remove和和empty的区别
    jQuery中的鼠标离开事件mouseout和mouseleave区别
    java类中的布尔(boolean&Boolean)类型字段要注意get方法和字段的命名
    Navicat_Premium_v15 激活
    navicat注册过期修改方法
    ClassNotFoundException找不到类异常的原因package 路径eclipse自动给我在路径前面加了一个java变成了java.com.XXX
    我保存一份博客园样式代码
  • 原文地址:https://www.cnblogs.com/xiaoSY-learning/p/5817268.html
Copyright © 2011-2022 走看看