SpringBoot + Itext7 html转pdf 解决分页,表格被分割留出空白,水印,页眉页脚页码。
Itext7 html转pdf 解决分页,表格被分割留出空白,水印,页眉页脚页码。可以解决表格内容在当页展示不下时被挤到下一页留出很大的一片空白。使用itext7的html2pdf插件。引入jar包<!--html转pdf工具 itext7 html2pdf --><dependency><groupId>com.itextpdf</groupId>
·
Itext7 html转pdf 解决分页,表格被分割留出空白,水印,页眉页脚页码。
可以解决表格内容在当页展示不下时被挤到下一页留出很大的一片空白。
使用itext7的html2pdf插件。
引入jar包
<!--html转pdf工具 itext7 html2pdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>3.0.5</version>
</dependency>
工具类
import cn.cooptec.admin.core.utils.pdfmarker.PdfHeaderMarker;
import cn.cooptec.admin.core.utils.pdfmarker.PdfPageMarker;
import cn.cooptec.admin.core.utils.pdfmarker.PdfWaterMarker;
import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.attach.impl.OutlineHandler;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.IBlockElement;
import com.itextpdf.layout.element.IElement;
import com.itextpdf.layout.font.FontProvider;
import com.itextpdf.layout.property.AreaBreakType;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.util.List;
import java.util.Map;
/**
* 导出PDF工具类
*/
public class PDFUtil {
private PDFUtil() {
}
private volatile static Configuration configuration;
static {
if (configuration == null) {
synchronized (PDFUtil.class) {
if (configuration == null) {
configuration = new Configuration(Configuration.VERSION_2_3_28);
}
}
}
}
/**
* freemarker 通过流的方式 引擎渲染 html
*
* @param dataMap 传入 html 模板的 Map 数据
* @param ftlFilePath html 模板文件相对路径(相对于 resources路径,路径 + 文件名),之后通过 BufferedReader 流来读取模板
* eg: "templates/pdf_export_demo.ftl"
* @return
*/
public static String freemarkerRender(Map<String, Object> dataMap, String ftlFilePath) {
configuration.setDefaultEncoding("UTF-8");
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setLogTemplateExceptions(false);
configuration.setWrapUncheckedExceptions(true);
Writer out = new StringWriter();
try {
Template template = new Template("", PDFUtil.returnReaderStream(ftlFilePath), configuration);
template.process(dataMap, out);
out.flush();
return out.toString();
} catch (IOException | TemplateException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 使用 iText 生成 PDF 文档
*
* @param htmlTmpStr html 模板文件字符串
* @param fontFilePath 所需字体文件(相对路径+文件名)
* @param waterImgPath 水印图片路径
* @param waterContent 水印文字内容
*/
public static byte[] createPDF(String htmlTmpStr, String fontFilePath, String waterImgPath, String waterContent) {
byte[] result = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PdfWriter writer = new PdfWriter(outputStream, new WriterProperties().setFullCompressionMode(Boolean.TRUE));
PdfDocument doc = new PdfDocument(writer);
try {
String waterText = "水印";
if (StringUtils.isNotBlank(waterContent)) {
String[] split = waterContent.split("-");
waterText = split[0] + "-" + split[1];
}
doc.setDefaultPageSize(PageSize.A4);
FontProvider fontProvider = new FontProvider();
// 设置中文字体文件的路径,可以在classpath目录下
fontProvider.addFont(fontFilePath, PdfEncodings.IDENTITY_H);
// 获取字体,提供给水印 和 页码使用
PdfFont pdfFont = fontProvider.getFontSet()
.getFonts()
.stream()
.findFirst()
.map(fontProvider::getPdfFont)
.orElse(null);
// 添加页眉
doc.addEventHandler(PdfDocumentEvent.START_PAGE, new PdfHeaderMarker(pdfFont, "页眉"));
// 添加水印
doc.addEventHandler(PdfDocumentEvent.INSERT_PAGE, new PdfWaterMarker(pdfFont, waterText, waterImgPath));
// 添加页脚
doc.addEventHandler(PdfDocumentEvent.END_PAGE, new PdfPageMarker(pdfFont));
ConverterProperties properties = new ConverterProperties();
fontProvider.addStandardPdfFonts();
fontProvider.addSystemFonts();
properties.setFontProvider(fontProvider);
// PDF目录
properties.setOutlineHandler(OutlineHandler.createStandardHandler());
// 将html转为pdf代码块,按div生成每天一页pdf
Document document = new Document(doc);
List<IElement> iElements = HtmlConverter.convertToElements(htmlTmpStr, properties);
int size = iElements.size();
for (int i = 0; i < size; i++) {
IElement iElement = iElements.get(i);
document.add((IBlockElement) iElement);
if (!(i == size - 1)) { // 不是最后一页
// 添加新的一页
document.add(new AreaBreak(AreaBreakType.NEXT_PAGE));
}
}
document.close();
result = outputStream.toByteArray();
} finally {
try {
doc.close();
writer.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 根据文件相对路径返回一个 BufferedReader 流
*
* @param filePath
* @return
* @throws IOException
*/
public static BufferedReader returnReaderStream(String filePath) throws IOException {
return new BufferedReader(new InputStreamReader(new ClassPathResource(filePath).getInputStream()));
}
/**
* 根据文件相对路径返回一个 BufferedReader 流
*
* @param filePath
* @return
* @throws IOException
*/
public static InputStream returnInputStream(String filePath) throws IOException {
return new ClassPathResource(filePath).getInputStream();
}
}
页眉
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
public class PdfHeaderMarker implements IEventHandler {
private PdfFont pdfFont;
private String title;
public PdfHeaderMarker(PdfFont pdfFont, String title) {
this.pdfFont = pdfFont;
this.title = title;
}
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
int pageNumber = pdf.getPageNumber(page);
if (pageNumber != 1) { // 封面不展示页眉页脚
Rectangle pageSize = page.getPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), pdf);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
float y = pageSize.getTop() - 25;
Paragraph p = new Paragraph(title)
.setFontSize(12)
.setFont(pdfFont);
// 顶部中间位置
canvas.showTextAligned(p, x, y, TextAlignment.CENTER);
canvas.close();
}
}
}
页脚页码
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
public class PdfPageMarker implements IEventHandler {
private PdfFont pdfFont;
public PdfPageMarker(PdfFont pdfFont) {
this.pdfFont = pdfFont;
}
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
int pageNumber = pdf.getPageNumber(page);
if (pageNumber != 1) { // 封面不展示页眉页脚
pageNumber = pageNumber - 1;
Rectangle pageSize = page.getPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), pdf);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
float y = pageSize.getBottom() + 15;
Paragraph p = new Paragraph("第" + pageNumber + "页")
.setFontSize(12)
.setFont(pdfFont);
// 底部中间位置
canvas.showTextAligned(p, x, y, TextAlignment.CENTER);
canvas.close();
}
}
}
水印
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.layout.property.VerticalAlignment;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class PdfWaterMarker implements IEventHandler {
private PdfFont pdfFont;
private String waterContent;
private String waterImgPath;
public PdfWaterMarker(PdfFont pdfFont, String waterContent, String waterImgPath) {
this.pdfFont = pdfFont;
this.waterContent = waterContent;
this.waterImgPath = waterImgPath;
}
@Override
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfDocument pdf = docEvent.getDocument();
PdfPage page = docEvent.getPage();
Rectangle pageSize = page.getPageSize();
PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), pdf);
Canvas canvas = new Canvas(pdfCanvas, pageSize);
Paragraph waterMarker = new Paragraph(waterContent)
.setFont(pdfFont)
.setOpacity(.5f)
.setFontSize(13);
// 右下角位置
canvas.showTextAligned(waterMarker, pageSize.getRight() - 150, pageSize.getBottom() + 15, pdf.getNumberOfPages(), TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0);
// 水印图片
Image waterImg = null;
if (StringUtils.isNotBlank(waterImgPath)) {
try {
InputStream inputStream = returnInputStream(waterImgPath);
ImageData waterImgData = ImageDataFactory.create(toByteArray(inputStream));
waterImg = new Image(waterImgData);
} catch (IOException e) {
e.printStackTrace();
}
}
// 图片水印
if (waterImg != null) {
int length = waterContent.length();
// 设置坐标 X Y
waterImg.setFixedPosition(pdf.getNumberOfPages(), pageSize.getRight() - (168 + length), pageSize.getBottom() + 12);
// 设置等比缩放
waterImg.scaleAbsolute(20, 20);// 自定义大小
// 写入图片水印
canvas.add(waterImg);
}
canvas.close();
}
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
public static InputStream returnInputStream(String filePath) throws IOException {
return new ClassPathResource(filePath).getInputStream();
}
}
配置文件
# pdf export config
pdfExport:
font: "static/fonts/simsun.ttc,0"
pdfTemplate: "templates/logModel/pdf_export_template.ftl"
waterImg: "static/images/watermark.png"
更多推荐
已为社区贡献1条内容
所有评论(0)