1、引入maven依赖包

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
2、案例(通过路径下载)
/**
 * @Version 1.0.0
 * @Description
 */
@Controller
public class PdfAction {

    /**
     * 报价单案例
     * @param response
     * @throws Exception
     */
    @RequestMapping("/downloadGhdPdf")
    public void  downloadGhdPdf(HttpServletResponse response) throws Exception{
        //新建一个临时文件
        // 存储pdf 文件
        String pdfFolder ="/tmp";
        File pdfFile = new File(pdfFolder);
        if (!pdfFile.exists()) {
            pdfFile.mkdir();
        }
        String pdf = pdfFolder + File.separator + UUID.randomUUID() + ".pdf";
        File file1 = new File(pdf);
        System.out.println("文件路径:"+file1.getAbsolutePath());

        Document document = new Document(PageSize.A4, 0, 0, 50, 0);
        FileOutputStream fos = new FileOutputStream(file1);
        PdfWriter writer = PdfWriter.getInstance(document,fos);

        document.open();
        //中文字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

        //正常字体
        Font normalFont = new Font(bfChinese, 10, Font.NORMAL);//SUPPRESS
        //中等大小
        Font midFont = new Font(bfChinese, 11, Font.NORMAL);
        //放大字体
        Font bigFont = new Font(bfChinese, 14, Font.BOLD);//SUPPRESS
        //标题
        Font titleFont = new Font(bfChinese,10,Font.NORMAL);
        //红色字体
        Font redFont = new Font(bfChinese,10,Font.NORMAL);
        redFont.setColor(BaseColor.RED);
        //单元格底色 BaseColor(235,235,235)
        BaseColor cellColor = new BaseColor(235,235,235);
        //

        Paragraph title = new Paragraph("xxxx",bigFont);
        Paragraph title1 = new Paragraph("xxx单",bigFont);
        title.setAlignment(1);
        title1.setAlignment(1);

        //图片
        //String imgUrl = "C:/Users/zwk/Desktop/zz.png";
        String imgUrl = "http://106.15.207.128:8072/resources/zz.png";
        Image image1 = Image.getInstance(imgUrl);
        //设置图片位置
        image1.setAbsolutePosition(420,740);
        //设置图片大小
        image1.scaleAbsolute(150,50);
        document.add(image1);

        //表格1
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(92); //宽度100%填充
        table.setSpacingBefore(20f);//设置间距
        //table.setSpacingAfter(10f);



        List<PdfPRow> listRow = table.getRows();
        //设置列宽
        float[] columnWidths = {1f,1f};
        table.setWidths(columnWidths);

        //行1
        PdfPCell cells1[] = new PdfPCell[2];
        PdfPRow headRow1 = new PdfPRow(cells1);

        //单元格
        cells1[0] = new PdfPCell(new Paragraph(" xx号:Q0004     ",titleFont ));//单元格内容

        cells1[1] = new PdfPCell(new Paragraph(" xx员:王无 ",titleFont ));
        cells1[0].setFixedHeight(20);//设置单元格高度
        //设置颜色
        cells1[0].setBackgroundColor(cellColor);
        cells1[1].setBackgroundColor(cellColor);

        //行2
        PdfPCell headtitleCells[] = new PdfPCell[2];
        PdfPRow headRow2 = new PdfPRow(headtitleCells);
        //单元格
        String headLeftText = " 车牌号:浙A0001\n vin码:123455678911\n 车型:奥迪";
        String headRightText = " 报价单流水号:123456789123456-1\n 报价单生成日期:2020-08-31\n 是否含票:否";
        Paragraph leftPara =  new Paragraph(headLeftText,midFont  );

        headtitleCells[0] = new PdfPCell(leftPara);//单元格内容
        headtitleCells[1] = new PdfPCell(new Paragraph(headRightText,midFont  ));
        headtitleCells[0].setFixedHeight(60);


        listRow.add(headRow1);
        listRow.add(headRow2);

        //数据表格2
        PdfPTable table2 = new PdfPTable(12);
        table2.setWidthPercentage(92); //宽度100%填充
        table2.setSpacingBefore(10f); //距离前前面多少
        table2.setSpacingAfter(10f);

        List<PdfPRow> listRow2 = table2.getRows();
        //设置列宽
        float[] columnWidths2 = {1f,2f,2f,1f,1f,1f,1f,1f,1f,1f,1f,1f};
        table2.setWidths(columnWidths2);

        //行1
        PdfPCell[] titleCells = new PdfPCell[12];
        PdfPRow titleRow = new PdfPRow(titleCells);
        //单元格
        titleCells[0] = new PdfPCell(new Paragraph("序号",titleFont));
        titleCells[1] = new PdfPCell(new Paragraph("配件名称",titleFont));//单元格内容
        titleCells[2] = new PdfPCell(new Paragraph("配件编码",titleFont));
        titleCells[3] = new PdfPCell(new Paragraph("原厂",titleFont));//单元格内容
        titleCells[4] = new PdfPCell(new Paragraph("现货",titleFont));
        titleCells[5] = new PdfPCell(new Paragraph("下线",titleFont));//单元格内容
        titleCells[6] = new PdfPCell(new Paragraph("现货",titleFont));
        titleCells[7] = new PdfPCell(new Paragraph("品牌",titleFont));//单元格内容
        titleCells[8] = new PdfPCell(new Paragraph("现货",titleFont));
        titleCells[9] = new PdfPCell(new Paragraph("其他",titleFont));//单元格内容
        titleCells[10] = new PdfPCell(new Paragraph("现货",titleFont));
        titleCells[11] = new PdfPCell(new Paragraph("备注",titleFont));//单元格内容
        //设置单元格颜色
        titleCells[0].setBackgroundColor(cellColor);
        titleCells[1].setBackgroundColor(cellColor);
        titleCells[2].setBackgroundColor(cellColor);
        titleCells[3].setBackgroundColor(cellColor);
        titleCells[4].setBackgroundColor(cellColor);
        titleCells[5].setBackgroundColor(cellColor);
        titleCells[6].setBackgroundColor(cellColor);
        titleCells[7].setBackgroundColor(cellColor);
        titleCells[8].setBackgroundColor(cellColor);
        titleCells[9].setBackgroundColor(cellColor);
        titleCells[10].setBackgroundColor(cellColor);
        titleCells[11].setBackgroundColor(cellColor);
        titleCells[0].setFixedHeight(18);

        listRow2.add(titleRow);
        for(int i = 0; i < 100; i++){
            PdfPCell dataCells[] = new PdfPCell[12];
            PdfPRow dataRow = new PdfPRow(dataCells);
            String xuhao = String.valueOf((i+1));
            dataCells[0] = new PdfPCell(new Paragraph(xuhao,normalFont));
            dataCells[1] = new PdfPCell(new Paragraph("配件名称",normalFont));//单元格内容
            dataCells[2] = new PdfPCell(new Paragraph("配件编码",normalFont));
            dataCells[3] = new PdfPCell(new Paragraph("原厂",normalFont));//单元格内容
            dataCells[4] = new PdfPCell(new Paragraph("现货",normalFont));
            dataCells[5] = new PdfPCell(new Paragraph("下线",normalFont));//单元格内容
            dataCells[6] = new PdfPCell(new Paragraph("现货",normalFont));
            dataCells[7] = new PdfPCell(new Paragraph("品牌",normalFont));//单元格内容
            dataCells[8] = new PdfPCell(new Paragraph("现货",normalFont));
            dataCells[9] = new PdfPCell(new Paragraph("其他",normalFont));//单元格内容
            dataCells[10] = new PdfPCell(new Paragraph("现货",normalFont));
            dataCells[11] = new PdfPCell(new Paragraph("备注",normalFont));//单元格内容
            dataCells[0].setFixedHeight(18);
            listRow2.add(dataRow);
        }

        //底部文本
        String bottom = "注意:xxxxx";
        Paragraph bottomPara = new Paragraph(bottom,redFont);
        bottomPara.setSpacingBefore(10);//距前面距离
        bottomPara.setIndentationLeft(20);//缩进

        document.add(title);
        document.add(title1);
        document.add(table);
        document.add(table2);
        document.add(bottomPara);
        document.close();


        String name = URLEncoder.encode("报价单.pdf", "utf-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-msdownload");
        response.addHeader("Content-Disposition", "attachment;filename*=utf-8'zh_cn'" + name);
        try (OutputStream out = response.getOutputStream()) {
            InputStream is = new FileInputStream(pdf);
            byte[] bys = new byte[1024];//SUPPRESS
            int len = 0;
            while ((len = is.read(bys)) != -1) {//SUPPRESS
                out.write(bys, 0, len);//SUPPRESS
            }
            is.close();
        } finally {
            if(file1.exists()){
                //删除临时文件
                file1.delete();
            }
        }
    }

}

  111

posted on 2020-09-04 08:42  lazyli  阅读(557)  评论(0编辑  收藏  举报