导入jar包,这两个jar包组合版本不能改,会报错
public String printPDF() throws Exception{
//创建文档
Document document = new Document(PageSize.A4, 15, 15, 15, 15);
//设置输出位置
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, bos);
//打开文档
document.open();
//内容
//创建根字体
BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont, AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
/*******大标题********/
//设置字体
Font BigTitleFont = new Font(baseFont, 24, Font.BOLD, BaseColor.BLACK);
//设置内容
Paragraph BigTitleparagraph = new Paragraph("出货表", BigTitleFont);
//设置对齐方式
BigTitleparagraph.setAlignment(Paragraph.ALIGN_CENTER);
//放入文档
document.add(BigTitleparagraph);
/*********小标题**********/
Font titleFont = new Font(baseFont, 12, Font.BOLD, BaseColor.BLACK);
//设置内容
//新建一个8列的表格
PdfPTable table = new PdfPTable(8);
//设置当前面table的上边距
table.setSpacingBefore(20f);
String[] titles = {"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
for (String s : titles) {
table.addCell(new PdfPCell(new Phrase(s, titleFont)));
}
/***********内容**********/
Font font = new Font(baseFont, 12, Font.NORMAL, BaseColor.BLACK);
//获取对应船期的货物
String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-mm')=?";
List<ContractProduct> cpList = contractProductService.find(hql, ContractProduct.class, new String[]{inputDate});
//将数据循环写入
for(ContractProduct cp:cpList){
table.addCell(new PdfPCell(new Phrase(cp.getContract().getCustomName(), font)));
table.addCell(new PdfPCell(new Phrase(cp.getContract().getContractNo(), font)));
table.addCell(new PdfPCell(new Phrase(cp.getProductNo(), font)));
table.addCell(new PdfPCell(new Phrase(cp.getCnumber()+"", font)));
table.addCell(new PdfPCell(new Phrase(cp.getFactoryName(), font)));
table.addCell(new PdfPCell(new Phrase(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod()), font)));
table.addCell(new PdfPCell(new Phrase(UtilFuns.dateTimeFormat(cp.getContract().getShipTime()), font)));
table.addCell(new PdfPCell(new Phrase(cp.getContract().getTradeTerms(), font)));
}
//添加至文档
document.add(table);
//关闭文档
document.close();
//下载
DownloadUtil downloadUtil = new DownloadUtil();
downloadUtil.download(bos, ServletActionContext.getResponse(), "出货表.pdf");
bos.close();
return NONE;//写NONE否则会因为DownloadUtil工具类报response重复响应
}