gpt4 book ai didi

spring - Flying Saucer 、 thymeleaf 和 Spring

转载 作者:IT老高 更新时间:2023-10-28 13:52:46 31 4
gpt4 key购买 nike

我有一个 Spring 应用程序,需要构建对 PDF 生成的支持。我正在考虑将飞碟与 Thymeleaf 一起使用来呈现 PDF。但是,我找不到太多关于将飞碟与 Thymeleaf 一起使用的信息。有其他人一起使用这些技术吗?

最佳答案

我正在使用带有 Thymeleaf 2.0.14 的 Flyingsaucer-R8 没有问题(我确信当前版本的 Thymeleaf 也可以正常工作)。

我有单独的 TemplateEngine 和为此目的配置的类路径模板解析器。使用它来生成 XHTML 作为 String。 Flyingsauc 然后根据结果创建 PDF 文档。检查下面的示例。

Code below is example - NOT PRODUCTION ready code use it with NO WARRANTY. For sake of clarity there's no try-catch handling and no resources caching (creating PDF is quite expensive operation). Consider that.

代码

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

import org.springframework.core.io.ClassPathResource;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

public class FlyingSoucerTestService {

public void test() throws DocumentException, IOException {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("META-INF/pdfTemplates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("XHTML");
templateResolver.setCharacterEncoding("UTF-8");

TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);

Context ctx = new Context();
ctx.setVariable("message", "I don't want to live on this planet anymore");
String htmlContent = templateEngine.process("messageTpl", ctx);

ByteOutputStream os = new ByteOutputStream();
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();

ClassPathResource regular = new ClassPathResource("/META-INF/fonts/LiberationSerif-Regular.ttf");
fontResolver.addFont(regular.getURL().toString(), BaseFont.IDENTITY_H, true);

renderer.setDocumentFromString(htmlContent);
renderer.layout();
renderer.createPDF(os);

byte[] pdfAsBytes = os.getBytes();
os.close();

FileOutputStream fos = new FileOutputStream(new File("/tmp/message.pdf"));
fos.write(pdfAsBytes);
fos.close();
}
}

模板

<?xml version="1.0" encoding="utf-8"?>

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring3-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style>
div.border {
border: solid;
border-width: 1px 1px 0px 1px;
padding: 5px 20px 5px 20px;
}
</style>
</head>
<body style="font-family: Liberation Serif;">

<div class="border">
<h1 th:text="${message}">message</h1>
</div>

</body>
</html>

关于spring - Flying Saucer 、 thymeleaf 和 Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23173485/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com