将带有html标签的字符串转为pdf文件

将带有html标签的字符串转为pdf文件

1、概述

大家好,我是欧阳方超,可以关注我的公众号“欧阳方超”,后续内容将在公众号首发。
最近一个项目有数据需要从第三方厂商获取,第三方厂商的接口返回的文本在cdata标签中,注意是带有p、span等标签的,我的目标是把它提取出来,并基于提取的内容生成pdf文件。

2、提取过程

下面是从三方厂商获取的带有html标签的字符串:

<![CDATA[ <p> &nbsp; </p> <p style="TEXT-INDENT: 42px"> <span style="FONT-FAMILY: times new roman; FONT-SIZE: 21px">9</span><span style="FONT-SIZE: 21px">月 ]]>

要实现转为pdf文件的功能,可以使用iText实现,所以需要引入下面的依赖:

<!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.7</version>
        <type>pom</type>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.itextpdf/itext7-core -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.1.7</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>io</artifactId>
        <version>7.1.7</version>
    </dependency>

    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.1.7</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>forms</artifactId>
        <version>7.1.7</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdfa</artifactId>
        <version>7.1.7</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>pdftest</artifactId>
        <version>7.1.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.itextpdf/html2pdf -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.1.4</version>
    </dependency>

接着使用HtmlConverter.convertToPdf()方法就能把相应的字符串转为pdf文件,具体代码为:

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;

import java.io.FileOutputStream;
import java.io.IOException;

public class HtmlToPdfConverterOne {
    public static void main(String[] args) throws IOException {
        String htmlContent = "<h1>Test</h1><p>Hello</p>"; // 这里放入你提供的 HTML 内容

        String pdfOutput = "D:\\test\\output.pdf"; // PDF 输出文件名

        ConverterProperties properties = new ConverterProperties();
        properties.setFontProvider(new DefaultFontProvider(true, true, true));
        // 使用 HtmlConverter 将 HTML 内容转换为 PDF
        HtmlConverter.convertToPdf(htmlContent, new FileOutputStream(pdfOutput), properties);

    }

接着出现了另一个问题,只要带有html标签的字符串中有中文,这些中文在转成的pdf文件中一律不显示,这可能是字体的问题导致中文不显示。iText 默认使用 Helvetica 字体,但它可能不包含中文字符。为了支持中文,你可以使用包含中文字体的版本。

以下是修改的建议:

添加中文字体依赖:在项目中添加支持中文的字体,比如 fireflysung。可以在 Maven 或 Gradle 中添加以下依赖:

<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>7.1.1</version>
</dependency>

设置字体:在转换之前,设置字体。这可以通过创建 ConverterProperties 对象并设置字体来完成:

ConverterProperties properties = new ConverterProperties();
properties.setFontProvider(new DefaultFontProvider(true, true, true));
HtmlConverter.convertToPdf(HTML, new FileOutputStream(DEST), properties);

这将启用默认的字体提供程序,并使用中文字体来支持中文字符。

请注意,确保 fireflysung 字体在你的项目中可用,或者可以选择其他包含中文字符的字体。之后就可以把中文显示在pdf文件中了。

3、总结

本文展示了把带html标签的字符串转为pdf的方法,并解决了过程中遇到的问题。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。
在这里插入图片描述

  • 23
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值