首先放一个自己写的pdfUtil,小伙伴们可以直接拷到项目里,没有代码侵染
package com.cpicdg.util.pdf;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.UnitValue;
import org.springframework.util.ResourceUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
/**
* @author zyy
* @program secl
* @description pdf工具类
* @date 2020-10-12 16:52
**/
public class PdfUtil {
/**
* 功能描述 构建文档对象
*
* @param filePath 生成的pdf路径
* @param pageSize pdf纸张大小
* @return com.itextpdf.layout.Document
* @date 2020/10/12 16:57
*/
public static Document generatorDoc(String filePath, PageSize pageSize) throws FileNotFoundException {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(filePath));
return new Document(pdfDoc, pageSize);
/*
* 构建完文档对象后,可以设置文档的标题等
* .setTitle等
*
* */
}
/**
* 功能描述 构建Table表格
*
* @param width 表格每一个列的宽度,支持多个参数,几个参数代表有几列
* @return com.itextpdf.layout.element.Table
* @date 2020/10/12 17:11
*/
public static Table generatorTable(float... width) {
//构建表格以100%的宽度
return new Table(width).setWidth(UnitValue.createPercentValue(100));
}
/**
* 功能描述 构建某一行的数据
*
* @param table 要构建的数据所处的行
* @param data 要构建的数据
* @return void
* @date 2020/10/13 9:40
*/
public static void generatorRow(Table table, String data) {
Cell cell = new Cell();
cell.add(new Paragraph(data).setTextAlignment(TextAlignment.CENTER));
table.addCell(cell);
}
public static void generatorRow(Table table, String data, int rowSpan, int colSpan) {
Cell cell = new Cell(rowSpan, colSpan);
cell.add(new Paragraph(data).setTextAlignment(TextAlignment.CENTER));
table.addCell(cell);
}
/**
* 功能描述 构建图片对象
*
* @param path 图片路径
* @return com.itextpdf.layout.element.Image
* @date 2020/10/12 17:23
*/
public static Image generatorImage(String path) throws MalformedURLException {
return new Image(ImageDataFactory.create(path));
}
/**
* 功能描述 构建字体
*
* @return com.itextpdf.kernel.font.PdfFont
* @date 2020/10/12 16:59
*/
public static PdfFont generatorFont() throws IOException {
//中文字体
String resourcePath = ResourceUtils.getURL("classpath:static").getPath();
String path = resourcePath + "/font/simhei.ttf";
//bfChinese = BaseFont.createFont(resourcePath.substring(0, resourcePath.indexOf("classes") + 7) + "/static/font/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
return PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H, true);
//return PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
}
}
具体用法 ,我这里是预览所以直接new PdfWriter(response.getOutputStream());如果要下载直接在上方添加
String fileName = "XXXX";
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("gb2312"), "8859_1") + ".pdf");
// (1)生成document,document可以理解为五子棋的棋盘,棋盘固定后,往上面落子
PdfWriter pdfWriter = new PdfWriter(response.getOutputStream());
PdfDocument pdfDoc = new PdfDocument(pdfWriter);
Document document = new Document(pdfDoc, PageSize.A4);
// 设置字体
document.setFont(PdfUtil.generatorFont());
// 添加图片,这里我最上面添加两个图片用来做右上角logo和左上角logo
// (2)获取图片对象
String staticPath = ResourceUtils.getURL("classpath:static").getPath();
// (2.1)添加左侧logo
Image logoLeft = PdfUtil.generatorImage(staticPath + "/images/cpic_logo_left.png");
// (2.1.1)缩放图片
logoLeft.scaleToFit(150, 80);
// (2.1.2)图片位置
logoLeft.setFixedPosition(40, 793);
// (2.1.3)设置图片
document.add(logoLeft);
// (2.2)添加右侧logo
Image logoRight = PdfUtil.generatorImage(staticPath +"/images/cpic_logo_right.png");
logoLeft.scaleToFit(200, 35);
logoRight.setFixedPosition(290, 793);
document.add(logoRight);
// (3)设置标题
// 设置第一个标题,标题在logo后面添加
Paragraph firstTitle = new Paragraph("保险事故线上查勘联络单");
// (3.1)设置标题与图标之间的间距
firstTitle.setMarginTop(25);
// (3.2)设置字体大小
firstTitle.setFontSize(23);
firstTitle.setTextAlignment(TextAlignment.CENTER);
document.add(firstTitle);
// (4)构建table
Table table = PdfUtil.generatorTable(80, 150, 150, 150, 150);
// (4.1)设置table字体
table.setFont(PdfUtil.generatorFont());
table.setMaxWidth(530);
// (5) 生成cell,也就是table中的每个格子的内容
// (5.1)第一行
PdfUtil.generatorRow(table, "出险时间");
document.add(table);
document.close();
public static void generatorRow(Table table, String data) {
Cell cell = new Cell();
cell.add(new Paragraph(data).setTextAlignment(TextAlignment.CENTER));
table.addCell(cell);
}
具体不懂可以下面评论,具体交流~