【问题标题】:Flying saucer, Thymeleaf and Spring飞碟、百里香叶和春天
【发布时间】:2014-06-04 02:33:01
【问题描述】:

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

【问题讨论】:

    标签: spring pdf thymeleaf flying-saucer


    【解决方案1】:

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

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

    下面的代码是示例 - 不生产准备好的代码使用它NO WARRANTY。为了清楚起见,没有 try-catch 处理和资源缓存(创建 PDF 是非常昂贵的操作)。考虑一下。

    代码

    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>
    

    【讨论】:

    • @michal.kreuzman 希望你仍然和我们在一起,因为这个解决方案帮助了我一点。一切顺利。以上引用:ctx.setVariable("message", "I don't want to live on this planet anymore");
    • 这种方法也没有解决国际化问题,使用 messages.properties 进行翻译。除此之外,它确实有效。
    • 您好,您在哪里存储此模板以供访问?我正在尝试访问 src/main/resources,但遇到了一些问题
    猜你喜欢
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多