itext提供可HTML转PDF的工具包,但是在中文字体时也许会遇到意想不到的问题。特别宋体对应的粗体。
0x01:引入itext包
com.itextpdf
itext7-core
7.1.11
pom
com.itextpdf
html2pdf
2.0.2
0x02:要转换的HTML文件
iText
大发发发萨法发发呆发呆东方大厦发
Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的
Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的
iText
大发发发萨法发发呆发呆东方大厦发
Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的
Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的
0x03:编写代码
import java.io.IOException;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
public class TextFooterEventHandler implements IEventHandler {
private Document doc;
private int page;
public TextFooterEventHandler(Document doc) {
this.doc = doc;
}
public void handleEvent(Event event) {
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
PdfCanvas canvas = new PdfCanvas(docEvent.getPage());
Rectangle pageSize = docEvent.getPage().getPageSize();
page++;
canvas.beginText();
try {
canvas.setFontAndSize(PdfFontFactory.createFont(FontConstants.HELVETICA_OBLIQUE), 15);
} catch (IOException e) {
e.printStackTrace();
}
canvas.moveText((pageSize.getRight() - doc.getRightMargin() +
(pageSize.getLeft() + doc.getLeftMargin())) / 2,
pageSize.getBottom() + doc.getBottomMargin())
.showText("" + page).endText().release();
}
}
itext默认字体
public static void defaultFont() throws Exception{
try {
String pdfPath = "C:\\Users\\2230\\Desktop\\a\\defaultFont.pdf";
PdfWriter pdfWriter = new PdfWriter(new File(pdfPath));
PdfDocument pdfDocument = new PdfDocument(pdfWriter) ;
Document doc = new Document(pdfDocument);
pdfDocument.setDefaultPageSize(PageSize.A4);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
ConverterProperties pro = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
fontProvider.addSystemFonts();
pro.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(new FileInputStream("C:\\Users\\2230\\Desktop\\a\\d.html"), pdfDocument, pro);
} catch (Exception e) {
e.printStackTrace();
}
}
默认转换结果
加载宋体
这里只加载了宋体,没有加载宋粗体
public static void songTiDefaultFont() throws Exception{
try{
String pdfPath = "C:\\Users\\2230\\Desktop\\a\\songTiDefaultFont.pdf";
PdfWriter pdfWriter = new PdfWriter(new File(pdfPath));
PdfDocument pdfDocument = new PdfDocument(pdfWriter) ;
Document doc = new Document(pdfDocument);
pdfDocument.setDefaultPageSize(PageSize.A4);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
ConverterProperties pro = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
//宋体
FontProgram fontProgram1 = FontProgramFactory.createFont("C:\\Users\\2230\\Desktop\\a\\simsun.ttc,1");
fontProvider.addFont(fontProgram1);
pro.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(new FileInputStream("C:\\Users\\2230\\Desktop\\a\\d.html"), pdfDocument, pro);
} catch (Exception e) {
e.printStackTrace();
}
}
发现转换出来的PDF,丢失了字体样式
分析原因发现C:\Windows\Fonts默认没有宋体粗字体,默认只有宋体常规
网上下载宋体粗字体宋体-粗体.ttf,下载的都是ttf格式;代码加载宋体粗字体
public static void songTiDefaultFont() throws Exception{
try{
String pdfPath = "C:\\Users\\2230\\Desktop\\a\\songTiDefaultFont.pdf";
PdfWriter pdfWriter = new PdfWriter(new File(pdfPath));
PdfDocument pdfDocument = new PdfDocument(pdfWriter) ;
Document doc = new Document(pdfDocument);
pdfDocument.setDefaultPageSize(PageSize.A4);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
ConverterProperties pro = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
//宋体
FontProgram fontProgram1 = FontProgramFactory.createFont("C:\\Users\\2230\\Desktop\\a\\simsun.ttc,1");
fontProvider.addFont(fontProgram1);
//宋体粗体
FontProgram fontProgram2 = FontProgramFactory.createFont("C:\\Users\\2230\\Desktop\\a\\宋体-粗体.ttf");
fontProvider.addFont(fontProgram2);
pro.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(new FileInputStream("C:\\Users\\2230\\Desktop\\a\\d.html"), pdfDocument, pro);
} catch (Exception e) {
e.printStackTrace();
}
}
多加了如下代码:
执行直接报错,字体无法加载
把ttf转换成otf格式字体
在线转换工具:https://www.fontke.com/tool/convfont
public static void songConvertTiFont() throws Exception{
try{
String pdfPath = "C:\\Users\\2230\\Desktop\\a\\songTiFont.pdf";
PdfWriter pdfWriter = new PdfWriter(new File(pdfPath));
PdfDocument pdfDocument = new PdfDocument(pdfWriter) ;
Document doc = new Document(pdfDocument);
pdfDocument.setDefaultPageSize(PageSize.A4);
pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE, new TextFooterEventHandler(doc));
ConverterProperties pro = new ConverterProperties();
FontProvider fontProvider = new DefaultFontProvider();
fontProvider.addSystemFonts();
//宋体
FontProgram fontProgram1 = FontProgramFactory.createFont("C:\\Users\\2230\\Desktop\\a\\simsun.ttc,0");
fontProvider.addFont(fontProgram1);
//宋体粗体
FontProgram fontProgram2 = FontProgramFactory.createFont("C:\\Users\\2230\\Desktop\\a\\宋体-粗体.otf");
fontProvider.addFont(fontProgram2);
pro.setFontProvider(fontProvider);
HtmlConverter.convertToPdf(new FileInputStream("C:\\Users\\2230\\Desktop\\a\\d.html"), pdfDocument, pro);
} catch (Exception e) {
e.printStackTrace();
}
}
转换结果