Java编写freemarker转PDF

路径问题值得注意,我采用的是通过类找的路径,读取freemarker代码要求也很严格

package com.yiyon.assets.utils;

import com.itextpdf.text.DocumentException;
import com.yiyon.assets.entity.AssetsRepair;
import com.yiyon.assets.web.ViewProgress;
import freemarker.template.Configuration;
import freemarker.template.Template;
import net.roseboy.jeee.workflow.entity.Task;
import net.roseboy.jeee.workflow.service.AuditBussService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.itextpdf.text.pdf.BaseFont;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

public class JavaToPdfFreemarker {
    @Autowired
    private AuditBussService auditBussService;
    private static final String DEST = "F:/test.pdf";
    private static final String HTML = "print-repair.ftl";
//    private static final String FONT = "simhei.ttf";
    private static final String LOGO_PATH = "file://"+/*PathUtil.getCurrentPath()+*/"/logo.png";

    private static Configuration freemarkerCfg = null;

    static {
        freemarkerCfg =new Configuration();
        //freemarker的模板目录
        try {
            freemarkerCfg.setClassForTemplateLoading(JavaToPdfFreemarker.class,"/templates");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
        //获取当前路径F:\yiyon\yiyon-server-tianjintaida
//        System.getProperty("user.dir");
        AssetsRepair bill = new AssetsRepair();
        bill.setCode("ZCWX20200710");
        bill.setBillDate(new Date());
        bill.setDemand("dfjjakfjakfksajf");
        bill.setBudget(new BigDecimal(20000));
        List<Task> tasks = new ArrayList<>();
        Map<String,Object> data = new HashMap();
        data.put("bill",bill);
        data.put("list", ViewProgress.getList(tasks));
        String content = JavaToPdfFreemarker.freeMarkerRender(data,HTML);
        JavaToPdfFreemarker.createPdf(content,DEST);
    }

    /**
     * freemarker渲染html
     */
    public static String freeMarkerRender(Map<String, Object> data, String htmlTmp) {
        Writer out = new StringWriter();
        try {
            // 获取模板,并设置编码方式
            Template template = freemarkerCfg.getTemplate(htmlTmp);
            template.setEncoding("UTF-8");
            // 合并数据模型与模板
            template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
            out.flush();
            return out.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return null;
    }

    public static void createPdf(String content,String dest) throws IOException, DocumentException, com.lowagie.text.DocumentException {
        ITextRenderer render = new ITextRenderer();
        ITextFontResolver fontResolver = render.getFontResolver();
//        fontResolver.addFont(FONT, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        // 解析html生成pdf
        render.setDocumentFromString(content);
        //解决图片相对路径的问题
//        render.getSharedContext().setBaseURL(LOGO_PATH);
        render.layout();
        render.createPDF(new FileOutputStream(dest));
    }
}

对于freemarker不显示中文的问题,除了引入字体,在标签上加是style="font-family=’ ‘’"之外,没发现其他的方法,但这种方法也不是完全管用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值