Springboot Thymeleaf Html转Pdf

新建项目

说明

用itextpdf写pdf,样式实在是太折磨了,这里选用Thymeleaf模板生成html转pdf,html+css写样式排版好太多了。

引入依赖


<dependency>
    <groupId>com.itextpdfgroupId>
    <artifactId>html2pdfartifactId>
    <version>4.0.2version>
dependency>

配置Thymeleaf

application.yml

spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML
    encoding: UTF-8

创建字体目录以及Pdf模板

创建以下文件

src/main/resources/font/cn.ttf *(cn.ttf必须是中文字体)
src/main/resources/templates/pdf.html

pdf.html

DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>Titletitle>
    <style>
        body {
            font-family: "Microsoft YaHei UI";
        }
    style>
head>
<body>
<h1>检测报告h1>
<div>
    <p>
        <span>ID:span>
        <span>007span>
    p>
    <p>
        <span>来源:span>
        <span th:text="${sys}">span>
    p>
    <table border="1">
        <tr style="background-color: #f6f6f6">
            <th>idth>
            <th>nameth>
            <th>ageth>
        tr>
        <tr>
            <td>1td>
            <td>tomtd>
            <td>20td>
        tr>
    table>
div>
body>
html>

创建Controller

TestControlle

package top.yxlhsx.thymeleafdemo;

import com.itextpdf.html2pdf.ConverterProperties;
import com.itextpdf.html2pdf.HtmlConverter;
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.layout.font.FontProvider;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {
	@Resource
    private TemplateEngine templateEngine;

    @GetMapping("pdf")
    public void importPdf() throws IOException{
        String outputFolder = "C:\\Users\\你的用户名\\Desktop\\新建文件夹\\test.pdf";
        OutputStream outputStream = Files.newOutputStream(Paths.get(outputFolder));

        Context context = new Context();
        context.setVariable("sys", "linux");

        String htmlStr = templateEngine.process("pdf", context);

        ConverterProperties properties = new ConverterProperties();
        FontProvider fontProvider = new DefaultFontProvider();
        FontProgram fontProgram = FontProgramFactory.createFont("font/cn.ttf");
        fontProvider.addFont(fontProgram);
        properties.setFontProvider(fontProvider);

        HtmlConverter.convertToPdf(htmlStr, outputStream, properties);
    }

}

注意

如果生成Html字符串时报 【java.lang.ClassNotFoundException: ognl.DefaultMemberAccess】 异常



<dependency>
    <groupId>com.itextpdfgroupId>
    <artifactId>html2pdfartifactId>
    <version>4.0.2version>
dependency>

Pdf效果

Springboot Thymeleaf Html转Pdf_第1张图片

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