不知道怎么描述,直接贴了代码了。有问题可以加Q群790537003讨论。先贴效果图:
controller直接调用下面方法 就能直接下载pdf到本地了
public static void invoicePdf(HttpServletResponse response, List<Map<String, Object>> data) {
Document document = new Document(new Rectangle(285, 375),-30,-30,0,0); //左 右 上 下
try {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/pdf");
// 下载文件的默认名称
SimpleDateFormat sdf = TimeUtils.sdf("yyyyMMddHHmmss");
response.setHeader("Content-Disposition", "attachment;filename=Invoice" + sdf.format(new Date()) + ".pdf");
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
//字体设置
//由于itext不支持中文,所以需要进行字体的设置,我这里让itext调用windows系统的中文字体,
//找到文件后,打开属性,将文件名及所在路径作为字体名即可。
//创建BaseFont对象,指明字体,编码方式,是否嵌入
BaseFont bf = BaseFont.createFont("/font/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//创建Font对象,将基础字体对象,字体大小,字体风格
document.open();
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/";
for(Map<String, Object> map : data) {
createTableInvoice(document, map, path);
}
/*int size = wrList.size();
int pageSize = 37; //每页大小
int pageTotal = size/pageSize+1; //总页数
for(int i=1; i<=pageTotal; i++) {
int startNum = (i-1)*pageSize+1; //第i页开始编号
int endNum = i*pageSize > size ? size : i*pageSize; //第i也结束编号
document.add(createTable1(document, font, font1, wrList, startNum, endNum));
}*/
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 发票每一页的内容
*/
public static void createTableInvoice(Document document, Map<String, Object> map, String path) throws Exception {
OrderInfo order = (OrderInfo)map.get("order");
Invoice invoice = (Invoice)map.get("invoice");
List<InvoiceDetail> details = (List<InvoiceDetail>)map.get("details");
document.newPage();
//创建BaseFont对象,指明字体,编码方式,是否嵌入
BaseFont bf = BaseFont.createFont("/font/arialbd.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//创建Font对象,将基础字体对象,字体大小,字体风格
Font font_6_normal = new Font(bf, 6, Font.NORMAL);
Font font_6_blod = new Font(bf, 6, Font.BOLD);
Font font_7_normal = new Font(bf, 7, Font.NORMAL);
Font font_7_bold = new Font(bf, 7, Font.BOLD);
// Font font_8_normal = new Font(bf, 8, Font.NORMAL);
// Font font_10_bold = new Font(bf, 10, Font.BOLD);
// Font font_10_normal = new Font(bf, 10, Font.NORMAL);
Font font_12_bold = new Font(bf, 12, Font.BOLD);
// Font font_12_normal = new Font(bf, 12, Font.NORMAL);
// Font font_14_bold = new Font(bf, 14, Font.BOLD);
//table1
PdfPTable table1 = new PdfPTable(1);
//设置每列宽度比例
int[] width1 = {100};
table1.setWidths(width1);
table1.getDefaultCell().setBorder(0);
table1.addCell(getPDFVerticalHorizontal("INVOICE",font_12_bold));
document.add(table1);
//添加空行
Paragraph blankRow1 = new Paragraph(5f, " ", font_6_normal);
document.add(blankRow1);
document.add(new LineSeparator(0.5f, 100, BaseColor.BLACK, 1, 0)); //添加黑色水平线
document.add(blankRow1); //添加空行
//table2
PdfPTable table2 = new PdfPTable(2);
int[] width2 = {15,85};
table2.setWidths(width2);
table2.getDefaultCell().setBorder(0);
table2.addCell(getPDFHorizontalLeft("Sold By:",font_7_bold));
table2.addCell(getPDFHorizontalLeft("Sino India Etail Private Limited",font_7_normal));
table2.addCell(getPDFHorizontalLeft(" ",font_7_normal));
table2.addCell(getPDFHorizontalLeft("F9A, Pinnacle Bussiness Park,",font_7_normal));
table2.addCell(getPDFHorizontalLeft(" ",font_7_normal));
table2.addCell(getPDFHorizontalLeft("Mahakali Caves Road, Andheri East, Mumbai 400093",font_7_normal));
table2.addCell(getPDFHorizontalLeft(" ",font_7_normal));
table2.addCell(getPDFHorizontalLeft("GSTN# 27AAYCS3582N1Z8",font_7_normal));
table2.addCell(getPDFHorizontalLeft(" ",font_7_normal));
table2.addCell(getPDFHorizontalLeft("CIN# U51909MH2017PTC292681",font_7_normal));
document.add(table2);
document.add(blankRow1); //添加空行
document.add(new LineSeparator(0.6f, 100, BaseColor.BLACK, 1, 0)); //添加黑色水平线
//table3
PdfPTable table3 = new PdfPTable(2);
int[] width3 = {50,50};
table3.setWidths(width3);
table3.getDefaultCell().setBorder(0);
table3.addCell(getPDFColspanLeft("Shipping/Billing Address",font_7_bold,2));
table3.addCell(getPDFColspanLeft(order.getCustomerName(),font_7_normal,2)); //"Customer Name: "+
String shippingAddress = order.getCustomerAddress1() + ',' +order.getCustomerAddress2();
shippingAddress = shippingAddress.replace("null", "");
table3.addCell(getPDFColspanLeft(shippingAddress,font_7_normal,2));
table3.addCell(getPDFNullCell());
table3.addCell(getPDFNullCell());
table3.addCell(getPDFHorizontalLeft("Place of Supply: "+invoice.getPlaceOfSupply(),font_7_normal));
table3.addCell(getPDFHorizontalLeft(order.getCustomerPincode(),font_7_normal));
table3.addCell(getPDFNullCell());
table3.addCell(getPDFNullCell());
document.add(table3);
document.add(new LineSeparator(0.6f, 100, BaseColor.BLACK, 1, 0)); //添加黑色水平线
/*//table4
PdfPTable table4 = new PdfPTable(1);
int[] width4 = {100};
table4.setWidths(width4);
table4.getDefaultCell().setBorder(0);
table4.addCell(getPDFHorizontalLeft("PinCode: "+order.getCustomerPincode(),font_7_normal));
document.add(table4);
document.add(blankRow1); //添加空行*/
//table5
PdfPTable table5 = new PdfPTable(2);
int[] width5 = {50,50};
table5.setWidths(width5);
table5.getDefaultCell().setBorder(0);
table5.addCell(getPDFHorizontalLeft("ShippingRefNo# ",font_7_normal));
table5.addCell(getPDFHorizontalLeft("AWB# "+details.get(0).getAwbNo(),font_7_normal));
table5.addCell(getPDFHorizontalLeft("OrderId# "+order.getOrderNo(),font_7_normal));
table5.addCell(getPDFHorizontalLeft(" ",font_7_normal));
table5.addCell(getPDFHorizontalLeft("InvoiceNo# "+ invoice.getInvoiceNo(),font_7_normal));
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
table5.addCell(getPDFHorizontalLeft("Date# "+sdf.format(invoice.getInvoiceTime()),font_7_normal));
document.add(table5);
document.add(blankRow1); //添加空行
document.add(new LineSeparator(0.6f, 100, BaseColor.BLACK, 1, 0)); //添加黑色水平线
//table6
PdfPTable table6 = new PdfPTable(9);
//设置每列宽度比例
int width6[] = {5,14,9,6,15,12,12,12,15};
table6.setWidths(width6);
table6.getDefaultCell().setBorder(0);
//第一二行
table6.addCell(getPDFVerticalHorizontal(" ",font_6_normal));
table6.addCell(getPDFHorizontalLeft("Item Name",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("Tax",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("Qty",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("Taxable",font_6_blod));
table6.addCell(getPDFHorizontalCenter("Tax Amount",font_6_blod,3));
table6.addCell(getPDFVerticalHorizontal("Total",font_6_blod));
table6.addCell(getPDFVerticalHorizontal(" ",font_6_blod));
table6.addCell(getPDFHorizontalLeft("HSN/SAC",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("Rate",font_6_blod));
table6.addCell(getPDFVerticalHorizontal(" ",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("Amount",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("SGST",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("CGST",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("IGST",font_6_blod));
table6.addCell(getPDFVerticalHorizontal("Amount",font_6_blod));
//第三四行 Item name 单独一行 后面数据一行 (一条明细占两行)
int detailsSize = details.size();
for(int i=0;i<detailsSize;i++) {
InvoiceDetail detail = details.get(i);
table6.addCell(getPDFVerticalHorizontal((i-1+2)+"",font_6_normal));
table6.addCell(getPDFColspanLeft(detail.getItemName(),font_6_normal,8));
table6.addCell(getPDFVerticalHorizontal(" ",font_6_normal));
table6.addCell(getPDFHorizontalLeft(detail.getHsn(),font_6_normal));
String taxRate = (NumberUtils.mul(new BigDecimal(detail.getTaxRate()), new BigDecimal(100))).setScale(2)+"%";
table6.addCell(getPDFVerticalHorizontal(taxRate,font_6_normal));
table6.addCell(getPDFVerticalHorizontal("1",font_6_normal));
table6.addCell(getPDFVerticalHorizontal(detail.getTaxExcludedPriceStr(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(detail.getCgstStr(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(detail.getSgstStr(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(detail.getIgstStr(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(detail.getUnitPriceStr(),font_6_normal));
}
//最后一行
table6.addCell(getPDFVerticalHorizontal(" ",font_6_normal));
table6.addCell(getPDFVerticalHorizontal(" ",font_6_normal));
table6.addCell(getPDFVerticalHorizontal("Total",font_6_normal));
table6.addCell(getPDFVerticalHorizontal(detailsSize+"",font_6_normal));
table6.addCell(getPDFVerticalHorizontal(map.get("totalTaxableValue").toString(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(map.get("totalCgst").toString(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(map.get("totalSgst").toString(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(map.get("totalIgst").toString(),font_6_normal));
table6.addCell(getPDFVerticalHorizontal(map.get("totalAmount").toString(),font_6_normal));
document.add(table6);
document.add(new LineSeparator(0.6f, 100, BaseColor.BLACK, 1, 0)); //添加黑色水平线
//table7
PdfPTable table7 = new PdfPTable(1);
int[] width7 = {100};
table7.setWidths(width7);
table7.addCell(getPDFHorizontalLeft("* Prices Inclusive of all Taxes.",font_7_normal));
table7.addCell(getPDFNullCell());
table7.addCell(getPDFHorizontalLeft("This is computer generated invoice and does not required physical signature.",font_6_normal));
table7.addCell(getPDFHorizontalLeft("The products are intended for end-user consumption and are not for retail sale.",font_6_normal));
document.add(table7);
}
//去掉边框 合并单元格居中
public static PdfPCell getPDFHorizontalCenter(String string, Font font, int col) {
Paragraph paragraph = new Paragraph(string,font);
PdfPCell cell = new PdfPCell(paragraph);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(col);
cell.setBorder(0);
return cell;
}
//去掉边框 合并单元格居做
public static PdfPCell getPDFColspanLeft(String string, Font font, int col) {
Paragraph paragraph = new Paragraph(string,font);
PdfPCell cell = new PdfPCell(paragraph);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setColspan(col);
cell.setBorder(0);
cell.setPadding(0);
cell.setMinimumHeight(10);
return cell;
}
//左对齐 去掉边框
public static PdfPCell getPDFHorizontalLeft(String string, Font font) {
PdfPCell cell = new PdfPCell(new Paragraph(string,font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setPadding(0);
cell.setBorder(0);
cell.setMinimumHeight(10);
return cell;
}
//垂直居中 水平居右
public static PdfPCell getPDFHorizontalRight(String string, Font font) {
PdfPCell cell = new PdfPCell(new Paragraph(string,font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setBorder(0);
return cell;
}
//垂直居中 水平居中
public static PdfPCell getPDFVerticalHorizontal(String string, Font font) {
PdfPCell cell = new PdfPCell(new Paragraph(string,font));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(0);
cell.setPadding(0);
cell.setMinimumHeight(10);
return cell;
}
//垂直居中 水平居中 空cell 设置高度
public static PdfPCell getPDFNullCell() {
PdfPCell cell = new PdfPCell(new Paragraph(""));
cell.setBorder(0);
cell.setPadding(0);
cell.setMinimumHeight(2);
return cell;
}