java导出PDF格式文件

@Controller
@RequestMapping(value = "download")
public class BusiExportReceiptInfoController {

private static final Logger logger         = LoggerFactory.getLogger(BusiExportReceiptInfoController.class);

@Autowired
private BusiExportReceiptInfoService busiExportReceiptInfoService;


@RequestMapping(value = "/busiExportReceiptInfoController", method = RequestMethod.GET)
public void exportRecAmtConfirm(HttpServletResponse response, BusiExportReceiptInfoReqBO reqBO) throws Exception{

logger.info("reqBO=" + reqBO);
if(reqBO.getEntryNo() == null){
logger.error("入库单号[entryNo]不能为空。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("入库单号[entryNo]不能为空");
return;
}
//查询数据 
                                                                获取数据部分代码
BusiExportReceiptInfoRspBO rspBO = busiExportReceiptInfoService.exportReceiptInfo(reqBO);
BusiExportHeadReceiptInfoRspBO head = rspBO.getHead();
List<BusiExportRowReceiptInfoRspBO> rows = rspBO.getRows();
if(head == null || rows == null || rows.isEmpty()){
logger.error("查询无数据,请检查。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("查询无数据,请检查");
return;
}


以下是生成PDF格式代码

// 定义字体格式
BaseFont baseFont = null;
try { 仿宋字体

baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀

UniGB-UCS2-H   是编码,在iTextAsian.jar 中以cmap为后缀

H 代表文字版式是 横版, 相应的 V 代表 竖版

} catch (Exception e) {
logger.error("定义字体出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;

}

     字体     大小        粗体

Font titleFont = new Font( baseFont, 14, Font.BOLD);
Font contentFont = new Font(baseFont, 8, Font.NORMAL);

//定义列数
final int COLUMNS = 22;


// 创建pdf文档               页面大小                        边距
Document document = new Document( PageSize.A4.rotate(), -75, -75, 15, 40);
文档对象类型

//把pdf文档输出到流,供前端下载

String file_name = "物资采购验收入库单";

file_name = new String(file_name.getBytes(), "ISO-8859-1");// 将file_name用UTF-8编码格式(系统默认)得到字节数,再用ISO-8859-1编码格式编译成字符串。
response.setHeader("Content-Disposition", "attachment;filename="+file_name+".pdf");// http header头要求其内容必须为iso8859-1编码
response.setContentType("application/x-download");
PdfWriter writer;
PDFShowPageFooterEvent event;
try {
writer = PdfWriter.getInstance(document, response.getOutputStream());/ /初始化PDF输出对象
event = new PDFShowPageFooterEvent(PageSize.A4.rotate(), titleFont,titleFont );// 设置页脚
writer.setPageEvent(event); // 设置打印行为
} catch (Exception e) {
logger.error("把pdf写到输出流出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}

//打开pdf文档
document.open();
// PdfContentByte directContent = writer.getDirectContent();
// event.onOpenDocument(writer,document);
// event.onStartPage(writer, document);

// 标题部分
PdfPTable titleTablex = new PdfPTable(3);// 设置3个单元宽度表格,PdfTable的构造函数,传入一个列数为参数,表示这个表格有多少列
String filePath = BusiExportReceiptInfoController.class.getResource("/").getPath().replace("/WEB-INF/classes", "") + "/images/logo_zggd.png";
Image image = Image.getInstance(filePath);// 获取图标
image.scalePercent(50);// 图像缩小50%
PdfPCell titleCellx = new PdfPCell(image);// 创建一个单元格实例
titleCellx.setRowspan(3);// 用于设置一个单元格跨多行

titleCellx.setFixedHeight(50);//设置每一行的高度

titleCellx.setColspan(1);//用于设置一个单元格跨多列
titleCellx.setBorder(0);//无边框

titleCellx.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);// 水平居左
titleCellx.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);// 垂直居中
titleTablex.addCell(titleCellx);// 将此单元格加入表格
titleCellx.setPhrase(new Phrase(head.getCompanyName(), contentFont));// 表格标题
titleCellx.setRowspan(1);
titleCellx.setColspan(1);
titleCellx.setBorder(0);
titleCellx.setFixedHeight(8);
titleCellx.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
titleCellx.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
titleTablex.addCell(titleCellx);
titleCellx.setPhrase(null);
titleTablex.addCell(titleCellx);

titleCellx.setPhrase(new Phrase("物资采购验收入库单", titleFont));// 标题
titleCellx.setRowspan(1);
titleCellx.setColspan(1);
titleCellx.setBorder(0);
titleCellx.setFixedHeight(20);
titleCellx.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
titleCellx.setVerticalAlignment(PdfPCell.ALIGN_BOTTOM);
titleTablex.addCell(titleCellx);
titleCellx.setPhrase(null);
titleTablex.addCell(titleCellx);

titleCellx.setPhrase(new Phrase(head.getEntryDate(), contentFont));// 入库时间
titleCellx.setRowspan(1);
titleCellx.setColspan(1);
titleCellx.setBorder(0);
titleCellx.setFixedHeight(8);
titleCellx.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
titleCellx.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
titleTablex.addCell(titleCellx);
titleCellx.setPhrase(null);
titleTablex.addCell(titleCellx);
try {
document.add(titleTablex);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}

//第1行内容
PdfPTable table1 = new PdfPTable(4);
PdfPCell cell1 = new PdfPCell();
cell1.setColspan(0);
cell1.setBorder(0);
cell1.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
cell1.setVerticalAlignment(PdfPCell.ALIGN_RIGHT);
table1.addCell(cell1);
cell1.setPhrase(new Phrase("入库单号:" + head.getEntryNo(), contentFont));
cell1.setColspan(2);
table1.addCell(cell1);
cell1.setPhrase(new Phrase("金额单位:元", contentFont));
cell1.setColspan(2);
table1.addCell(cell1);
try {
document.add(table1);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}

//第2行内容
PdfPTable table2 = new PdfPTable(COLUMNS);
PdfPCell cell2 = new PdfPCell(new Phrase("厂商名称", contentFont));
cell2.setColspan(1);
cell2.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table2.addCell(cell2);
cell2.setPhrase(new Phrase(""+head.getPurchaseName(), contentFont));
cell2.setColspan(12);
table2.addCell(cell2);
cell2.setPhrase(new Phrase("合同号", contentFont));
cell2.setColspan(1);
table2.addCell(cell2);
cell2.setPhrase(new Phrase(""+head.getNotificationNo(), contentFont));
cell2.setColspan(8);
table2.addCell(cell2);
try {
document.add(table2);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}

//第3行内容
PdfPTable table3 = new PdfPTable(COLUMNS);
PdfPCell cell3 = new PdfPCell(new Phrase("需用单位", contentFont));
cell3.setColspan(1);
cell3.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell3.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table3.addCell(cell3);
cell3.setPhrase(new Phrase("", contentFont));
cell3.setColspan(12);
table3.addCell(cell3);
cell3.setPhrase(new Phrase("合同号", contentFont));
cell3.setColspan(1);
table3.addCell(cell3);
cell3.setPhrase(new Phrase("", contentFont));
cell3.setColspan(8);
table3.addCell(cell3);
try {
document.add(table3);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}


//表头
PdfPTable headTable = new PdfPTable(COLUMNS);
PdfPCell headCell = new PdfPCell(new Phrase("序号", contentFont));
headCell.setColspan(1);
headCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
headCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("发票号码", contentFont));
headCell.setColspan(2);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("物资名称", contentFont));
headCell.setColspan(8);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("规格 ", contentFont));
headCell.setColspan(1);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("型号 ", contentFont));
headCell.setColspan(1);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("单位", contentFont));
headCell.setColspan(1);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("数量", contentFont));
headCell.setColspan(1);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("单价", contentFont));
headCell.setColspan(2);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("货款", contentFont));
headCell.setColspan(2);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("税率", contentFont));
headCell.setColspan(1);
headTable.addCell(headCell);
headCell.setPhrase(new Phrase("税金", contentFont));
headCell.setColspan(2);
headTable.addCell(headCell);
try {
document.add(headTable);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}


//明细
for(BusiExportRowReceiptInfoRspBO row:rows)
{
PdfPTable detailTable = new PdfPTable(COLUMNS);
PdfPCell detailCell = new PdfPCell(new Phrase(""+row.getSeq(), contentFont));
detailCell.setColspan(1);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
detailCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getInvoiceNo(), contentFont));
detailCell.setColspan(2);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getItemName(), contentFont));
detailCell.setColspan(8);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getSpec(), contentFont));
detailCell.setColspan(1);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getModel(), contentFont));
detailCell.setColspan(1);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getUnitName(), contentFont));
detailCell.setColspan(1);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(""+row.getQuantity(), contentFont));
detailCell.setColspan(1);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getPurchaseUnitPrice(), contentFont));
detailCell.setColspan(2);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getNotTaxAmt(), contentFont));
detailCell.setColspan(2);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(""+row.getTaxPercent(), contentFont));
detailCell.setColspan(1);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
detailTable.addCell(detailCell);
detailCell.setPhrase(new Phrase(row.getTaxAmt(), contentFont));
detailCell.setColspan(2);
detailCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
detailTable.addCell(detailCell);
try {
document.add(detailTable);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}
}

//倒数第2行内容
PdfPTable table5 = new PdfPTable(COLUMNS);
PdfPCell cell5 = new PdfPCell(new Phrase("成本小计", contentFont));
cell5.setColspan(17);
cell5.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell5.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table5.addCell(cell5);
cell5.setPhrase(new Phrase(head.getSumNotTaxAmt(), contentFont));
cell5.setColspan(2);
cell5.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
table5.addCell(cell5);
cell5.setPhrase(new Phrase("", contentFont));
cell5.setColspan(1);
cell5.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT);
table5.addCell(cell5);
cell5.setPhrase(new Phrase(head.getSumTaxAmt(), contentFont));
cell5.setColspan(2);
table5.addCell(cell5);
try {
document.add(table5);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}



//倒数第1行内容
PdfPTable table6 = new PdfPTable(COLUMNS);
PdfPCell cell6 = new PdfPCell(new Phrase("成本合计", contentFont));
cell6.setColspan(17);
cell6.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell6.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
table6.addCell(cell6);
// cell6.setPhrase(new Phrase(""+head.getSumNotTaxAmt().add(head.getSumTaxAmt()), contentFont));
cell6.setPhrase(new Phrase(head.getSum(), contentFont));


cell6.setColspan(5);
table6.addCell(cell6);
try {
document.add(table6);
} catch (DocumentException e) {
logger.error("pdf追加table出错。reqBO=" + reqBO);
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("生成pdf出错");
return;
}
// event.onCloseDocument(writer, document);
//关闭pdf文档
document.close();


}

}




猜你喜欢

转载自blog.csdn.net/mouzu/article/details/78375560