springboot thymeleaf html转pdf两种实现

thymeleaf是用来获取html数据的,毕竟是个模版,传参最终拼成html的字符串string。

获取html传入给相关组件。在网上找了N多方式。itext5,pdfbox,puppeteer,还有个google header命令行。还有问前端有没有什么好的推荐,前端直接让后端来搞。后两个没测试,看着网上说的效果不错。给我的感觉就是截图,转pdf,具体我并没有实现。itext5需要前端调整它能支持的样式。

        最终选择,我的版本是最新的9.1.5

参考抄袭文章:

SpringBoot Thymeleaf企业级真实应用:使用Flying Saucer结合iText5将HTML界面数据转换为PDF输出(四) 表格中断问题_flying saucer html转pdf表格分页断开-CSDN博客

        
        
        
            org.xhtmlrenderer
            flying-saucer-pdf-itext5
            ${flying-saucer-pdf-itext5.version}
        

代码抄袭就不展示了,我主要是把thymleaf放到了业务工程,pdf转换是单独的插件。大致截图

springboot thymeleaf html转pdf两种实现_第1张图片

业务端:

springboot thymeleaf html转pdf两种实现_第2张图片

代码抄袭简单,我又试了另外一种,openhtmltopdf

         
            
            
                com.openhtmltopdf
                openhtmltopdf-core
                1.0.10
            
            
            
                com.openhtmltopdf
                openhtmltopdf-pdfbox
                1.0.10
            
            
            
                org.jsoup
                jsoup
                1.13.1
            
import com.openhtmltopdf.pdfboxout.PdfRendererBuilder;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;
import org.jsoup.nodes.Document;
import org.springframework.util.ResourceUtils;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class PdfUtil {
    public static void buildPdf( String htmlContent) throws Exception {
        Document document = Jsoup.parse(htmlContent);
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.useFont(ResourceUtils.getFile("classpath:font/SimSun.ttc"), "simsun");
        builder.useFont(ResourceUtils.getFile("classpath:font/msyh.ttc"), "msyh");
        builder.useFont(ResourceUtils.getFile("classpath:font/msyhl.ttc"), "msyhl");
        builder.useFont(ResourceUtils.getFile("classpath:font/msyhbd.ttc"), "msyhbd");
        builder.useFastMode();
//        builder.withHtmlContent(htmlContent, "").toString();
        builder.withW3cDocument(new W3CDom().fromJsoup(document), "");
        String outputPath = "D:\\svndocs\\4444.pdf";
        try (OutputStream os = new FileOutputStream(outputPath)) {
            builder.toStream(os);
            builder.run();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static void buildPdf2( String htmlContent) throws Exception {
        PdfRendererBuilder builder = new PdfRendererBuilder();
        builder.useFont(ResourceUtils.getFile("classpath:font/SimSun.ttc"), "simsun");
        builder.useFont(ResourceUtils.getFile("classpath:font/simsunb.ttf"), "simsunb");
        builder.useFont(ResourceUtils.getFile("classpath:font/msyh.ttc"), "msyh");

        builder.useFastMode();
        builder.withHtmlContent(htmlContent, "").toString();
        String outputPath = "D:\\svndocs\\55555.pdf";
        try (OutputStream os = new FileOutputStream(outputPath)) {
            builder.toStream(os);
            builder.run();
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

两种写法效果一样。

写博客记录肯定是有不一样的,那么切记,html里一定要带上字体,纸张大小也可以。甚至页眉页脚水印都可以通过css来设置.尤其openhtmltopdf,代码没有直接支持水印什么的,就是让你在HTML里用样式处理。oepnhtmltopdf比上边第一种方式简单,生成的pdf也比第一种清晰,不知道第一种哪里可以设置。毕竟我只会百度,下次遇到直接博客里抄.

    

你可能感兴趣的:(spring,boot,pdf,java)