html导出pdf

<!-- html转PDF -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>kernel</artifactId>
        <version>7.1.1</version>
    </dependency>
    
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>layout</artifactId>
        <version>7.1.1</version>
    </dependency>
    
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.0.1</version>
    </dependency>


public class ItextPDFUtil {

public static void main(String[] args) {
String htmlStr = null;
InputStream inputStream = null;
PdfDocument pd = null;
try {
// 读取html的流
inputStream = new FileInputStream(new File("F:/协议.html"));

// 流转换成字符串
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = inputStream.read(b)) != -1;) {
out.append(new String(b, 0, n));
}

htmlStr = out.toString();
String pdffile = "F:/test.pdf";

pd = new PdfDocument(new PdfWriter(new FileOutputStream(new File(pdffile))));
// 设置文件标题为fileName,web上展示的标题为此标题
pd.getDocumentInfo().setTitle(pdffile);
}
catch (Exception e) {
e.printStackTrace();
}

Document document = new Document(pd, PageSize.A4);
try {
// 导入字体
FontProvider font = new FontProvider();
font.addFont("F:/SimSun.ttf");

ConverterProperties c = new ConverterProperties();
c.setFontProvider(font);
c.setCharset("utf-8");

// 设置页面边距 必须先设置边距,再添加内容,否则页边距无效
document.setMargins(50, 0, 50, 0);
List<IElement> list = HtmlConverter.convertToElements(htmlStr, c);
for (IElement ie : list) {
document.add((IBlockElement) ie);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
document.close();
}

}

public static void htmlToPdf(String html, OutputStream outStream, String fontPath) {
PdfDocument pd = null;
Document document = null;
try {
pd = new PdfDocument(new PdfWriter(outStream));

// 导入字体
FontProvider font = new FontProvider();
font.addStandardPdfFonts();
font.addFont(fontPath);
ConverterProperties c = new ConverterProperties();
c.setFontProvider(font);
c.setCharset("utf-8");

// 设置页面边距 必须先设置边距,再添加内容,否则页边距无效
document = new Document(pd, PageSize.A4, true);
document.setMargins(50, 0, 40, 0);
List<IElement> list = HtmlConverter.convertToElements(html, c);
for (IElement ie : list) {
document.add((IBlockElement) ie);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
document.close();
}

}

转载于:https://www.cnblogs.com/shenggege5240/p/10063090.html

### Java 使用 HTML 导出 PDF 的方法及库 #### 1. iText 库结合 Thymeleaf 或 FreeMarker 渲染 HTML 后转换成 PDF iText 是一个强大的用于创建和操作 PDF 文档的 Java 库。可以先通过模板引擎如 Thymeleaf 或者 FreeMarker 将数据填充到 HTML 模板中生成完整的 HTML 页面,再利用第三方工具将此页面转为 PDF 文件。 对于依赖管理,在 Maven 中添加如下配置来引入最新版本的 iText 和所选模板引擎: ```xml <dependencies> <!-- iText --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core</artifactId> <version>7.x.x</version> <!-- 查找最新的稳定版号替换此处 --> </dependency> <!-- 如果选用Thymeleaf作为模板引擎 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.x.x</version> <!-- 查找最新的稳定版号替换此处 --> </dependency> <!-- 若选择FreeMarker则改为下面这行 --> <!--<dependency>--> <!-- <groupId>org.freemarker</groupId>--> <!-- <artifactId>freemarker</artifactId>--> <!-- <version>2.x.x</version> <!– 查找最新的稳定版号替换此处 –>--> <!--</dependency>--> </dependencies> ``` 完成上述设置之后就可以编写代码逻辑实现功能需求了[^3]。 #### 2. 使用 Flying Saucer (XHTMLRenderer) 直接解析 XHTMLPDF Flying Saucer 是另一个流行的开源项目,它能够直接读取符合标准的 XHTML 并将其渲染为高质量的 PDF 输出。这种方式不需要额外的数据绑定过程,适合于已经存在现成静态网页资源的情况。 Maven 配置如下所示: ```xml <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf-itext5</artifactId> <version>9.1.20</version> </dependency> ``` 简单示例代码片段展示如何加载本地文件路径下的 html 进而保存 pdf: ```java import org.xhtmlrenderer.pdf.ITextRenderer; public class HtmlToPdfExample { public static void main(String[] args) throws Exception{ String url = "file:///path/to/your/input.html"; String outputFileName = "/path/to/output.pdf"; ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(url); renderer.layout(); renderer.createPDF(outputFileName); System.out.println("成功导出PDF!"); } } ``` 以上两种方案各有优劣,具体取决于实际应用场景和个人偏好决定采用哪种方式[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值