使用 java Apache PDFBOX 添加 HTML 标记
Add HTML Markup using java Apache PDFBOX
我一直在使用 PDFBOX 和扩展 PDFBOX 的 EasyTable 来绘制数据表。我遇到了一个问题,我有一个 java 对象,其中包含一串 HTML 数据,我需要使用 PDFBOX 将其添加到 PDF 中。对文档的挖掘似乎没有任何成果。
下面的代码是一个 hello world 片段,我希望生成的 pdf 具有 H1 格式。
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "<h1>HelloWorld</h1>" );
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
}
PDFBox 不知道 HTML,至少不知道如何创建内容。
因此,对于纯 PDFBox,您必须自己解析 HTML 并从标签文本中导出特殊的文本绘制特征。
例如当您遇到 "<h1>HelloWorld</h1>"
时,您必须提取文本 "HelloWorld"
并将它在 h1
标记中的信息用于 select 适当的素数 header 字体和绘制 "HelloWorld"
.
的字体大小
或者,您可以寻找一个库来执行 HTML 解析和转换为 PDFBox 的 PDF 文本绘图指令,例如Open HTML to PDF.
使用 jerico 将 html 格式化为自由文本,同时正确映射标签的输出。
样本
public String extractAllText(String htmlText){
return new net.htmlparser.jericho
.Source(htmlText)
.getRenderer()
.setMaxLineLength(Integer.MAX_VALUE)
.setNewLine(null)
.toString();
}
包含在您的 gradle 或 maven 中:
compile group: 'net.htmlparser.jericho', name: 'jericho-html', version: '3.4'
我一直在使用 PDFBOX 和扩展 PDFBOX 的 EasyTable 来绘制数据表。我遇到了一个问题,我有一个 java 对象,其中包含一串 HTML 数据,我需要使用 PDFBOX 将其添加到 PDF 中。对文档的挖掘似乎没有任何成果。
下面的代码是一个 hello world 片段,我希望生成的 pdf 具有 H1 格式。
// Create a document and add a page to it
PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage( page );
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);
// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont( font, 12 );
contentStream.moveTextPositionByAmount( 100, 700 );
contentStream.drawString( "<h1>HelloWorld</h1>" );
contentStream.endText();
// Make sure that the content stream is closed:
contentStream.close();
// Save the results and ensure that the document is properly closed:
document.save( "Hello World.pdf");
document.close();
}
PDFBox 不知道 HTML,至少不知道如何创建内容。
因此,对于纯 PDFBox,您必须自己解析 HTML 并从标签文本中导出特殊的文本绘制特征。
例如当您遇到 "<h1>HelloWorld</h1>"
时,您必须提取文本 "HelloWorld"
并将它在 h1
标记中的信息用于 select 适当的素数 header 字体和绘制 "HelloWorld"
.
或者,您可以寻找一个库来执行 HTML 解析和转换为 PDFBox 的 PDF 文本绘图指令,例如Open HTML to PDF.
使用 jerico 将 html 格式化为自由文本,同时正确映射标签的输出。
样本
public String extractAllText(String htmlText){
return new net.htmlparser.jericho
.Source(htmlText)
.getRenderer()
.setMaxLineLength(Integer.MAX_VALUE)
.setNewLine(null)
.toString();
}
包含在您的 gradle 或 maven 中:
compile group: 'net.htmlparser.jericho', name: 'jericho-html', version: '3.4'