不知道怎么描述,直接贴了代码了。有问题可以加Q群790537003讨论。先贴效果图:
controller直接调用下面方法 就能直接下载pdf到本地了
public static void invoicePdf(HttpServletResponse response, List
/**
* 发票每一页的内容
*/
public static void createTableInvoice(Document document, Map map, String path) throws Exception {
OrderInfo order = (OrderInfo)map.get("order");
Invoice invoice = (Invoice)map.get("invoice");
List details = (List)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