java - 使用 java Apache PDFBOX 添加 HTML 标记

标签 java spring-boot pdfbox

我一直在使用 PDFBOX 和 EasyTable,它扩展了 PDFBOX 来绘制数据表。我遇到了一个问题,我有一个带有 HTML 数据字符串的 java 对象,我需要使用 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();

    }

最佳答案

使用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'

关于java - 使用 java Apache PDFBOX 添加 HTML 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58221833/

相关文章:

java - 手机号码字段验证

java - 如何在JUnit4中使用ClassRule启动Spring Boot服务器?

java - Jackson 未正确反序列化 SetValuedMap

java - 如何在java spring boot中使用多个join sql查询

java - 如何在Java中将两个PDF文件合并为一个?

java - 如何使用 PDFBox 将标准字体嵌入到生成的 PDF 中

java - 如何使用 PDFBox 应用波浪线?

java - 并行流和 forkjoinpool 在生产中使用安全吗?

java - 如何在此代码中显示负数阶乘

java - 奇怪的 Java 错误放置