使用freemaker和itext从html模板生成pdf文件

本文介绍了如何使用FreeMarker和iText库将HTML模板转换为PDF合同文档。在处理过程中,首先将Word文档手动转换为HTML,然后通过定义FreeMarker的实用方法进行PDF生成。为了解决中文显示问题,需要在HTML中设置body内字体为宋体。
摘要由CSDN通过智能技术生成

   这里主要需要生成一个合同的pdf文档,拿到的合同格式是word文档,为了便于处理,我们用DW将其内容手动转为html。

   所以接下来的问题主要就是如何从一个html的合同模板变成一个pdf文件。

   首先我们定义好freemaker的util方法,使其易于调用

public class TemplateUtils {

	private static FreeMarkerConfigurer templateEngine = new FreeMarkerConfigurer();

	static {
		try {
			templateEngine.setConfiguration(templateEngine.createConfiguration());
			TemplateLoader loader = new FileTemplateLoader(new ClassPathResource("template").getFile());
			templateEngine.getConfiguration().setTemplateLoader(loader);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}

	public static String generateTemplateContent(String templateName, Map map) {
		try {
			Template t = templateEngine
					.getConfiguration().getTemplate(templateName);
			return FreeMarkerTemplateUtils.processTemplateIntoString(t, map);
		} catch (Exception e) {
		
使用FreemarkeriText可以方便地将HTML转换为PDF。下面是一个基本的示例: 1. 首先,您需要引入iTextfreemarker的依赖项。如果您使用的是Maven,可以添加以下依赖项到pom.xml文件中: ``` <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> ``` 2. 创建Freemarker模板,例如: ``` <html> <head> <title>${title}</title> </head> <body> <h1>${heading}</h1> <p>${content}</p> </body> </html> ``` 3. 创建Java类来生成PDF文件。例如: ``` import java.io.*; import java.util.*; import com.lowagie.text.*; import com.lowagie.text.pdf.*; import freemarker.template.*; public class HtmlToPdf { public static void main(String[] args) throws Exception { // 创建数据模型 Map<String, Object> data = new HashMap<String, Object>(); data.put("title", "My PDF Document"); data.put("heading", "Hello World"); data.put("content", "This is my first PDF document."); // 创建Freemarker模板 Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setClassForTemplateLoading(HtmlToPdf.class, "/"); Template template = cfg.getTemplate("template.ftl"); // 将Freemarker模板渲染为HTML StringWriter writer = new StringWriter(); template.process(data, writer); String html = writer.toString(); // 创建PDF文档 Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); document.open(); // 将HTML转换为PDF InputStream is = new ByteArrayInputStream(html.getBytes()); XMLWorkerHelper.getInstance().parseXHtml(writer, document, is); // 关闭PDF文档 document.close(); } } ``` 这个示例将生成一个名为“output.pdf”的PDF文件,其中包含一个标题为“My PDF Document”的页面,以及一个标题为“Hello World”和内容为“This is my first PDF document.”的段落。 请注意,此示例仅是一个基本示例。您可以根据需要修改模板和Java代码以生成更复杂的PDF文件
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值