package com.dxhy.extend.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Locale;

import org.xhtmlrenderer.pdf.ITextRenderer;

//import com.lowagie.text.DocumentException;
//import com.lowagie.text.pdf.BaseFont;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

/**
 * @author dxhy
 */
public class ExtPdfUtils {
//    public static ITextRenderer getRender() throws DocumentException, IOException {
//
//        ITextRenderer render = new ITextRenderer();
//
//        // 添加字体，以支持中文
//        render.getFontResolver().addFont("/export/font/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//        render.getFontResolver().addFont("/export/font/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//
//        return render;
//    }

    /** 获取要写入PDF的内容 */
    public static String getPdfContent(String ftlName, Object o) throws IOException, TemplateException {
        return useTemplate(ftlName, o);
    }

    /** 使用freemarker得到html内容 */
    public static String useTemplate(String ftlName, Object o) throws IOException, TemplateException {

        String html = null;

        Template tpl = getFreemarkerConfig().getTemplate(ftlName);

        StringWriter writer = new StringWriter();
        tpl.process(o, writer);
        writer.flush();
        html = writer.toString();
        return html;
    }

    /**
     * 获取Freemarker配置
     *
     * @return 返回
     */
    private static Configuration getFreemarkerConfig() {
        freemarker.template.Version version = new freemarker.template.Version("2.3.22");
        Configuration config = new Configuration(version);
        config.setClassForTemplateLoading(ExtPdfUtils.class, "/export/report");
        config.setEncoding(Locale.CHINA, "utf-8");
        return config;
    }
}
