阅读文本大概需要3分钟。
itext提供可HTML转PDF的工具包,但是在中文字体时也许会遇到意想不到的问题。特别宋体对应的粗体。
0x01:引入itext包
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.11</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>2.0.2</version>
</dependency>
0x02:要转换的HTML文件
</head>
<p>iText</p>
<div><b>大发发发萨法发发呆发呆东方大厦发</b></div>
<div style="font-family:SimSun;weight:normal">Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的</div>
<div style="font-family:SimSun;font-weight:bold">Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的</div>
<p>iText</p>
<div><b>大发发发萨法发发呆发呆东方大厦发</b></div>
<div style="font-family:宋体;weight:normal">Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的</div>
<div style="font-family:宋体;font-weight:bold">Your developer here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的here法大大是地方大发顺丰萨法发顺丰的journey with iText begins here法大大是地方大发顺丰萨法发顺丰的</div>
</body>
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{
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();
public static void songTiDefaultFont() throws Exception{
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();
public static void songConvertTiFont() throws Exception{
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();