itext做PDF导出

项目要做PDF导出数据,还要实现点击在浏览器上下载的效果!于是去网上找有没有什么前人写好的工具包拿过来用,一看果然还是有的!我这边就直接贴我的实现代码吧,反正都是百度的!

导入依赖:

		<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.5.13</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>

工具类:

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.jeecg.wx.entity.WxDeliveryRecordEntity;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.List;

public class PdfReportUtils {
    
    
    public static void exportPdf(HttpServletResponse response,List<WxDeliveryRecordEntity> list) {
    
    
        //1,创建文档对象
        Document document = new Document(PageSize.A2);
        //2,监听输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStream out = null;
        try {
    
    
            PdfWriter.getInstance(document, baos);
            //3,打开文档
            document.open();
            //4,添加文本
            Paragraph paragraph = new Paragraph();
//            document.add(new Paragraph("pdf渲染好的文本",fontChinese));
            paragraph.setAlignment(1); //设置文字居中 0靠左   1,居中     2,靠右
            paragraph.setIndentationLeft(12); //设置左缩进
            paragraph.setIndentationRight(12); //设置右缩进
            paragraph.setFirstLineIndent(24); //设置首行缩进
            paragraph.setLeading(20f); //行间距
            paragraph.setSpacingBefore(5f); //设置段落上空白
            paragraph.setSpacingAfter(10f); //设置段落下空白

            // 表格
            PdfPTable table = createTable(new float[]{
    
    60, 40, 40, 40, 50, 60, 60, 120, 40});
            table.addCell(createCell("商品名称", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品单价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品数量", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品总价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品优惠价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品优惠后总价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品支付金额", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("交易时间", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("交易状态", keyfont, Element.ALIGN_CENTER));
            for (WxDeliveryRecordEntity entity : list) {
    
    
                table.addCell(createCell(entity.getShopName(), textfont));
                table.addCell(createCell(entity.getShopPrice().toString(), textfont));
                table.addCell(createCell(entity.getShopNum().toString(), textfont));
                table.addCell(createCell(entity.getShopTotalPrice().toString(), textfont));
                table.addCell(createCell(entity.getShopDiscountPrice().toString(), textfont));
                table.addCell(createCell(entity.getShopNewPrice().toString(), textfont));
                table.addCell(createCell(entity.getShopPayPrice().toString(), textfont));
                table.addCell(createCell(entity.getShopDeliveryTime().toString(), textfont));
                switch (entity.getShopDeliveryFlag()) {
    
    
                    case 1:
                        table.addCell(createCell("交易正常", textfont));
                        break;
                    case 2:
                        table.addCell(createCell("退单", textfont));
                        break;
                    case 3:
                        table.addCell(createCell("交易异常", textfont));
                        break;
                }
            }
//            paragraph.add(table);
            document.add(paragraph);
            document.add(table);
//            PdfReportUtils.generatePDF(document, list);
            //5,关闭文档
            document.close();
            //6,设置请求返回类型
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=shopRecord.pdf");
            response.setContentLength(baos.size());
            out = response.getOutputStream();
            baos.writeTo(out);
            out.flush();
            out.close();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        } /*finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
    }

    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;
    // 最大宽度
    private static int maxWidth = 650;
    static {
    
    
        try {
    
    
            // 不同字体(这里定义为同一种字体:包含不同字号、不同style)
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 16, Font.BOLD);
            headfont = new Font(bfChinese, 14, Font.BOLD);
            keyfont = new Font(bfChinese, 10, Font.BOLD);
            textfont = new Font(bfChinese, 10, Font.NORMAL);

        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
    public static PdfPCell createCell(String value, Font font) {
    
    
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建单元格(指定字体、水平..)
     *
     * @param value
     * @param font
     * @param align
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align) {
    
    
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建默认列宽,指定列数、水平(居中、右、左)的表格
     *
     * @param colNumber
     * @param align
     * @return
     */
    public PdfPTable createTable(int colNumber, int align) {
    
    
        PdfPTable table = new PdfPTable(colNumber);
        try {
    
    
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(align);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable createTable(float[] widths) {
    
    
        PdfPTable table = new PdfPTable(widths);
        try {
    
    
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return table;
    }
}

总结一下,我是抄了两篇博客再加上我自己以前用poi做Excel导出的一些思想逻辑!首先我是要做类似于表格那样的导出,但是客户需求要导出成PDF的。所以要先画表格!

PdfPTable table = createTable(new float[]{
    
    60, 40, 40, 40, 50, 60, 60, 120, 40});
            table.addCell(createCell("商品名称", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品单价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品数量", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品总价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品优惠价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品优惠后总价", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("商品支付金额", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("交易时间", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("交易状态", keyfont, Element.ALIGN_CENTER));

createTable是自定义的一个方法

    /**
     * 创建指定列宽、列数的表格
     *
     * @param widths
     * @return
     */
    public static PdfPTable createTable(float[] widths) {
    
    
        PdfPTable table = new PdfPTable(widths);
        try {
    
    
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        return table;
    }

可以看到这是定义一个数组,里面放着表格的宽度,要注意总宽度不要大于最大宽度!

控制层:

	/**
	 * 获取页面数据,并且把数据导出到pdf里
	 * @param entity
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "exportPdf")
	@ResponseBody
	public List<WxDeliveryRecordEntity> getDeliveryRecordList(WxDeliveryRecordEntity entity,HttpServletResponse response) throws Exception {
    
    
		List<WxDeliveryRecordEntity> list = wxDeliveryRecordService.getDeliveryRecordList(entity);
		PdfReportUtils.exportPdf(response,list);
		return list;
	}

我这个用的持久层是hibernate,总是你把数据封装到集合里然后传到工具类里面,然后去浏览器访问你写的接口,他就会下载了!

猜你喜欢

转载自blog.csdn.net/Curtisjia/article/details/106558899