//向模板里填数据
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();
}