效果图
继承PdfPageEventHelper重写onEndPage方法
package com.xuqiang.itext.test;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
/**
* @author xuqiang
* @date 2019/1/2
*/
public class PDFBuilder extends PdfPageEventHelper {
private Phrase leftHeader;
private Phrase rigntHeader;
static final int marginX = 90;
static final int marginY = 70;
private static BaseFont baseFont;
// 生成下划线空白占位符
private static String Blank;
// 页眉字体
private static Font font;
// 下划线字体
private static Phrase blankPhrase;
PDFBuilder(String[] header) {
this.leftHeader = new Phrase(header[0], PDFBuilder.font);
this.rigntHeader = new Phrase(header[1], PDFBuilder.font);
}
static {
try {
// 中文字体依赖itext得itext-asian包
baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 168; i++) {
sb.append("\u00a0");
}
Blank = sb.toString();
font = new Font(PDFBuilder.baseFont, 9, Font.UNDEFINED);
blankPhrase = new Phrase(PDFBuilder.Blank, new Font(PDFBuilder.baseFont, Font.DEFAULTSIZE, Font.UNDERLINE));
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param writer
* @param document
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
int yMargin = -20;
float top = document.top(yMargin);
// 第一页不生成页眉页脚
if (document.getPageNumber() == 1) {
return;
}
//生成下划线,使用空格占位
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, PDFBuilder.blankPhrase,
document.left(-1), top, 0);
//生成左侧页眉
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, leftHeader,
document.left(), top, 0);
//生成右侧页眉
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, rigntHeader,
document.right(), top, 0);
//生成页脚页数
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, new Phrase(String.valueOf(document.getPageNumber() - 1), PDFBuilder.font),
document.right(), document.bottom(yMargin), 0);
}
}
测试类
package com.xuqiang.itext.test;
import com.itextpdf.text.Document;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
/**
* @author xuqiang
* @date 2018/12/29
*/
public class Test {
public static void main(String[] args) throws Exception {
// 创建一个文档
Document document = new Document(PageSize.A4, PDFBuilder.marginX, PDFBuilder.marginX, PDFBuilder.marginY, PDFBuilder.marginY);
// pdf输出流
OutputStream outputStream = new FileOutputStream("E:\\Users\\Desktop\\工作\\吊车组杆方案模板\\吊车组杆方案-test.pdf");
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
// 添加页眉页脚
pdfWriter.setPageEvent(new PDFBuilder(new String[]{"新增糖厂坳配变解决110kV那前站10kV903线糖厂坡配变过载工程", "吊车组立电杆施工方案"}));
document.open();
// XMLWorkerHelper.getInstance().parseXHtml(
// pdfWriter,
// document,
// new ByteArrayInputStream(
// "<html><style></style><body style=\"font-family: SimSun;\"><h1>第一页1p开始</h1></body></html>"
// .getBytes(StandardCharsets.UTF_8)),
// StandardCharsets.UTF_8);
// html生成pdf,注意文件要是utf-8编码
// 不显示中文页面注意添加字体<body style=\"font-family: SimSun;\">
// 标签格式严格,注意使用在线格式化工具格式化html代码
XMLWorkerHelper.getInstance().parseXHtml(
pdfWriter,
document,
new FileInputStream("E:\\Users\\Desktop\\工作\\吊车组杆方案模板\\吊车组杆方案.htm"),
StandardCharsets.UTF_8);
document.close();
}
}
依赖
compile 'com.itextpdf:itextpdf:5.5.13'
compile 'com.itextpdf.tool:xmlworker:5.5.13'
compile 'com.itextpdf:itext-asian:5.2.0'
springMVC导出PDF,解决下载文件中文乱码
/**
* 导出PDF文件
* @param response
* @throws Exception
*/
@RequestMapping("apppdf")
public void exportPDF(HttpServletResponse response) throws Exception {
// ftl 路径
String path = "ftl/pdf/pdfTemplate.ftl";
// 读取网页
String html = Tools.readFileAllContent(path);
// 设置也没数据
String[] header = new String[]{"新增糖厂坳配变解决110kV那前站10kV903线糖厂坡配变过载工程", "吊车组立电杆施工方案"};
// 文件名
String filename = URLEncoder.encode(header[0] + ".pdf", "UTF-8");
// 文件名写入返回头,解决中文乱码
response.setHeader("Content-Disposition", "attachment;filename=" + filename + ";filename*=UTF-8''" + filename);
// 获取输出流
ServletOutputStream outputStream = response.getOutputStream();
// 生成pdf并写回
ExportPDFUtil.exportPDF(outputStream, html, header);
}
github地址