HTML生成PDF模板(Java iText+FreeMarker生成PDF(HTML转PDF))

本文介绍了如何使用Java的iText库和FreeMarker模板引擎将HTML内容转换为PDF文件。首先讲解了iText的基本用法,包括创建基本的PDF文档和处理中文字符。接着展示了如何通过iText的XMLWorkerHelper解析HTML,并结合FreeMarker动态生成HTML内容。最后提到了 Flying Saucer 这个库,它支持更高级的CSS样式解析,以及将PDF转换为图片的Jpedal组件。此外,文章还提及了PhantomJS作为纯前端的解决方案。
摘要由CSDN通过智能技术生成

Java iText+FreeMarker生成PDF(HTML转PDF)
1.背景
在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等。方便用户查看,下载,打印。目前常用的解决方案是,把相关数据信息,生成对应的pdf文件返回给用户。
在这里插入图片描述

本文源码:http://git.oschina.net/lujianing/java_pdf_demo
2.iText
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。
iText 官网:http://itextpdf.com/
iText 开发文档: http://developers.itextpdf.com/developers-home
iText目前有两套版本iText5和iText7。iText5应该是网上用的比较多的一个版本。iText5因为是很多开发者参与贡献代码, 因此在一些规范和设计上存在不合理的地方。iText7是后来官方针对iText5的重构,两个版本差别还是挺大的。不过在实际使用中,一般用到的都比较 简单,所以不用特别拘泥于使用哪个版本。比如我们在http://mvnrepository.com/中搜索iText,出来的都是iText5的依赖。
来个最简单的例子:
添加依赖:

com.itextpdf itextpdf 5.5.11 测试代码:JavaToPdf package com.lujianing.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**

  • Created by lujianing on 2017/5/7.
    */
    public class JavaToPdf {

    private static final String DEST = “target/HelloWorld.pdf”;

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
    document.open();
    document.add(new Paragraph(“hello world”));
    document.close();
    writer.close();
    }
    }
    运行结果:
    在这里插入图片描述

3.iText-中文支持
iText默认是不支持中文的,因此需要添加对应的中文字体,比如黑体simhei.ttf
可参考文档:http://developers.itextpdf.com/examples/font-examples/using-fonts#1227-tengwarquenya1.java
测试代码:JavaToPdfCN
package com.lujianing.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**

  • Created by lujianing on 2017/5/7.
    */
    public class JavaToPdfCN {

    private static final String DEST = “target/HelloWorld_CN.pdf”;
    private static final String FONT = “simhei.ttf”;

    public static void main(String[] args) throws FileNotFoundException, DocumentException {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
    document.open();
    Font f1 = FontFactory.getFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
    document.add(new Paragraph(“hello world,我是鲁家宁”, f1));
    document.close();
    writer.close();
    }
    }
    输出结果:
    在这里插入图片描述

4.iText-Html渲染
在一些比较复杂的pdf布局中,我们可以通过html去生成pdf
可参考文档:http://developers.itextpdf.com/examples/xml-worker-itext5/xml-worker-examples
添加依赖:

com.itextpdf.tool xmlworker 5.5.11 添加模板:template.html Title
你好,鲁家宁
测试代码:JavaToPdfHtml package com.lujianing.test;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;<

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,您想了解关于使用iTextFreeMarker生成HTML并将其换为Word文档的内容。首先,您需要了解几个概念: 1. iText是一个用于生成PDF文档的Java库,但它也可以用于生成其他类型的文档,例如Word文档。 2. FreeMarker是一个用于生成动态内容的Java模板引擎,它可以将数据模型与模板文件结合使用,生成静态文本文件。 接下来,您可以按照以下步骤来实现将HTML换为Word文档: 1. 创建一个FreeMarker模板文件,其中包含您要生成HTML内容,并将其保存为.ftl文件。 2. 在Java代码中,使用FreeMarker来读取.ftl文件并将数据模型填充到模板中,生成HTML字符串。 3. 使用iTextHTML字符串换为Word文档。您可以使用iText的XMLWorkerHelper类来实现此功能。 以下是一个简单的示例代码,用于将HTML字符串换为Word文档: ``` // Step 1: Load the FreeMarker template Configuration cfg = new Configuration(Configuration.VERSION_2_3_28); cfg.setDirectoryForTemplateLoading(new File("templates")); Template template = cfg.getTemplate("my_template.ftl"); // Step 2: Generate HTML string from the template and data model Map<String, Object> data = new HashMap<>(); data.put("name", "John Doe"); StringWriter out = new StringWriter(); template.process(data, out); String html = out.getBuffer().toString(); // Step 3: Convert HTML to Word document using iText ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new ByteArrayInputStream(html.getBytes())); document.close(); // Save the Word document to file FileOutputStream fos = new FileOutputStream("output.docx"); fos.write(baos.toByteArray()); fos.close(); ``` 请注意,此示例代码仅用于说明概念,并且可能需要进行修改以适合您的特定需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值