1. 程式人生 > >後臺列印pdf檔案

後臺列印pdf檔案

 不知道怎麼描述,直接貼了程式碼了。有問題可以加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;
    }