• 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();
        }
  • 相关阅读:
    SVG Stroke属性
    C# 线程同步之排它锁/Monitor监视器类
    在Mac OS X Yosemite 10.10.3 中搭建第一个 ASP.NET 5 Web 项目
    jquery 之 Deferred 使用与实现
    jQuery 之 Callback 实现
    在解决方案中所使用 NuGet 管理软件包依赖
    下载和使用 Open XML PowerTools
    下载和编译 Open XML SDK
    Open XML SDK 在线编程黑客松
    VS2013 解决方案文件结构分析
  • 原文地址:https://www.cnblogs.com/xiaoSY-learning/p/5817268.html
Copyright © 2020-2023  润新知