Java thymeleaf html 模板导出PDF

pom文件
9.1.18 3.0.11.RELEASE

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

pdfutils

package com.ys.utils;

import com.lowagie.text.pdf.BaseFont;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.FileTemplateResolver;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PdfUtils {

    public static void method() throws Exception{
//        String path = request.getSession().getServletContext().getRealPath("");
//        String filePath = path + env.getProperty("outputPath") + File.separator + UUIDUtil.getShortUuid() + ".pdf";
        OutputStream out = new FileOutputStream(new File("F:/sss.pdf"));
        Map locker = new HashMap<>();
        locker.put("lockerName", "lockerName");
        locker.put("exportDate", "exportDate");
        locker.put("paymentModeName", "paymentModeName");
        locker.put("paymentMothod", "paymentMothod");
        locker.put("paymentTotal", "paymentTotal");
        locker.put("boxSizeName", "boxSizeName");
        locker.put("boxSize", "boxSize");
        locker.put("boxSizeTotal", "boxSizeTotal");
        locker.put("locationName", "locationName");
        locker.put("location", "location");
        locker.put("locationTotal", "locationTotal");
        Map paymentList = new HashMap<>();
        List list = new ArrayList<>();
        paymentList.put("paymentMothod", "Alipay");
        paymentList.put("paymentTotal", 90.0);
        list.add(paymentList);
        Map boxSizeList = new HashMap<>();

        List boxlist = new ArrayList<>();
        boxSizeList.put("boxSize", "smaller");
        boxSizeList.put("boxSizeTotal", 30.0);
        boxlist.add(boxSizeList);
        Map locationList = new HashMap<>();

        List locationlist = new ArrayList<>();
        locationList.put("location", "New york");
        locationList.put("locationTotal", 20.0);
        locationlist.add(locationList);
        Map map = new HashMap<>();
        map.put("locker", locker);
        map.put("paymentList", list);
        map.put("boxSizeList", boxlist);
        map.put("locationList", locationlist);
//        String fileTemplatePath = path + env.getProperty("templatePath") + File.separator;
        String htmlStr = generate("F:/project/sourceysProject/pdfModel/", "LockerModel", map);

        DocumentBuilder builder =  DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(htmlStr.getBytes("UTF-8")));
        ITextRenderer renderer = new ITextRenderer();

        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont("F:/project/sourceysProject/pdfModel/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//        fontResolver.addFont("C:/Windows/Fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        renderer.setDocument(doc, null);
        renderer.layout();
        renderer.createPDF(out);
        out.close();
        // 下载文件
        String fileName = "test.pdf";
//        ExcelUtil.downloadFile(request, response, fileName, filePath);
    }

    public static String generate(String filePath, String template, Map variables) throws Exception{

        //创建模板解析器
        FileTemplateResolver templateResolver = new FileTemplateResolver();
        templateResolver.setPrefix(filePath);
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setTemplateMode("XHTML");
        templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
        templateResolver.setCacheable(true);

        //创建模板引擎并初始化解析器
        TemplateEngine engine = new TemplateEngine();
        engine.setTemplateResolver(templateResolver);
        engine.isInitialized();

        //输出流
        StringWriter stringWriter = new StringWriter();
        BufferedWriter writer = new BufferedWriter(stringWriter);

        //获取上下文
        Context ctx = new Context();
        ctx.setVariables(variables);
        engine.process(template,  ctx, writer);

        stringWriter.flush();
        stringWriter.close();
        writer.flush();
        writer.close();

        //输出html
        String htmlStr = stringWriter.toString();
        return htmlStr;
    }

    public static void main(String args[]) throws Exception {
          method();
//        String Str = generate("d:\\","test",new HashMap());
//        System.out.println(Str);
    }
}

html模板



    Thymeleaf!!!!!
    
    


你可能感兴趣的:(thyemleaf,html生成pdf)