1、pom文件中引入
com.itextpdf itextpdf 5.5.13 com.itextpdf itext-asian 5.2.0 com.itextpdf.tool xmlworker 5.5.11
2、pdf样式类
package com.agileplat.supplychain.util; import com.itextpdf.text.*; import com.itextpdf.text.pdf.BaseFont; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; public class PdfUtil { // 标准字体 public static Font NORMALFONT; // 加粗字体 public static Font BOLDFONT; //固定高 public static float fixedHeight = 27f; //间距 public static int spacing = 5; static { try { BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); NORMALFONT = new Font(bfChinese, 10, Font.NORMAL); BOLDFONT = new Font(bfChinese, 14, Font.BOLD); } catch (Exception e) { e.printStackTrace(); } } public static Document createDocument() { //生成pdf Document document = new Document(); // 页面大小 Rectangle rectangle = new Rectangle(PageSize.A4); // 页面背景颜色 rectangle.setBackgroundColor(BaseColor.WHITE); document.setPageSize(rectangle); // 页边距 左,右,上,下 document.setMargins(20, 20, 20, 20); return document; } /** * @param text 段落内容 * @return */ public static Paragraph createParagraph(String text, Font font,int space) { Paragraph elements = new Paragraph(text, font); elements.setSpacingBefore(space); elements.setSpacingAfter(space); // elements.setSpacingAfter(spacing); return elements; } public static Font createFont(int fontNumber, int fontSize, BaseColor fontColor) { //中文字体 ----不然中文会乱码 BaseFont bf = null; try { bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); return new Font(bf, fontNumber, fontSize, fontColor); } catch (Exception e) { e.printStackTrace(); } return new Font(bf, Font.DEFAULTSIZE, Font.NORMAL, BaseColor.BLACK); } /** * 隐藏表格边框线 * * @param cell 单元格 */ public static void disableBorderSide(PdfPCell cell) { if (cell != null) { cell.disableBorderSide(1); cell.disableBorderSide(2); cell.disableBorderSide(4); cell.disableBorderSide(8); } } /** * 创建居中得单元格 * * @return */ public static PdfPCell createCenterPdfPCell() { PdfPCell cell = new PdfPCell(); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setFixedHeight(fixedHeight); return cell; } /** * 创建指定文字得单元格 * * @param text * @return */ public static PdfPCell createCenterPdfPCell(String text, int rowSpan, int colSpan, Font font) { PdfPCell cell = new PdfPCell(new Paragraph(text, font)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setFixedHeight(fixedHeight); cell.setRowspan(rowSpan); cell.setColspan(colSpan); return cell; } /** * @param len 表格列数 * @return */ public static PdfPTable createPdfPTable(int len) { PdfPTable pdfPTable = new PdfPTable(len); pdfPTable.setSpacingBefore(5); pdfPTable.setHorizontalAlignment(Element.ALIGN_CENTER); return pdfPTable; } }
3、controller下载类
@GetMapping("/exportPDF/{id}") public void generatePDF(HttpServletResponse response,@PathVariable Long id) throws Exception { String filename = "测试pdf生成"; // 设置下载格式为pdf response.setContentType("application/x-download"); response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf"); cgddBzjService.generateCgddPDF(response); }
4、service实现类
public void generateCgddPDF(HttpServletResponse response) throws IOException, DocumentException { OutputStream os = new BufferedOutputStream(response.getOutputStream()); // 1. Document document = new Document(); Document document = PdfUtil.createDocument(); // 2. 获取writer PdfWriter.getInstance(document, os); // 3. open() document.open(); //设置字体 Font largeFont = PdfUtil.createFont(20, Font.NORMAL, BaseColor.BLACK); Font bigFont = PdfUtil.createFont(14, Font.NORMAL, BaseColor.BLACK); Font littleFont = PdfUtil.createFont(8, Font.NORMAL, BaseColor.BLACK); Font littleFontUnserline = PdfUtil.createFont(8, Font.UNDERLINE, BaseColor.BLACK); Paragraph title = PdfUtil.createParagraph("表头", largeFont,2); title.setAlignment(Element.ALIGN_CENTER); //定义数据的字体 BaseFont baseFont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); Font textFont = new Font(baseFont, 6, Font.NORMAL); //定义表格 PdfPTable sjtable = new PdfPTable(new float[] { 10,20,30,50,20,20,30,30,40,40}); sjtable.setTotalWidth(555); sjtable.setLockedWidth(true); sjtable.setHorizontalAlignment(Element.ALIGN_CENTER);//居左 PdfPCell heardCell = new PdfPCell(); heardCell.setVerticalAlignment(Element.ALIGN_MIDDLE); heardCell.setHorizontalAlignment(Element.ALIGN_CENTER); heardCell.setPhrase(new Phrase("序号", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("品牌", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("品名", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("规格", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("单位", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("数量", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("单价", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("金额", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("工号", textFont)); sjtable.addCell(heardCell); heardCell.setPhrase(new Phrase("货期", textFont)); sjtable.addCell(heardCell); PdfPCell endvalue = new PdfPCell(); endvalue.setFixedHeight(20f); endvalue.setColspan(2); endvalue.setPhrase(new Phrase("备注:", textFont)); PdfPCell endvalue2 = new PdfPCell(); endvalue2.setColspan(3); endvalue2.setPhrase(new Phrase("", textFont)); PdfPCell endvalue3 = new PdfPCell(); endvalue3.setColspan(2); endvalue3.setPhrase(new Phrase("合计(元):", textFont)); PdfPCell endvalue4 = new PdfPCell(); endvalue4.setPhrase(new Phrase("", textFont)); PdfPCell endvalue5 = new PdfPCell(); endvalue5.setPhrase(new Phrase("", textFont)); sjtable.addCell(endvalue); sjtable.addCell(endvalue2); sjtable.addCell(endvalue3); sjtable.addCell(endvalue4); sjtable.addCell(endvalue5); sjtable.addCell(endvalue5); // 4. 添加段落内容 document.add(title); document.add(sjtable); // 5. close() document.close(); os.close(); }