ftl 生成pdf 可打印、可下载

/**
     * 生成pdf可以下载、打印
     * @param request
     * @param response
     * @param pdfName 配置文件名称(html和pdf两个页面)
     * @param root  需要传到页面 的参数
     * @throws DocumentException
     * @throws IOException
     * @throws TemplateException
     */
public static void pdf(HttpServletRequest request,HttpServletResponse response,String pdfName,Map<String, Object> root) throws DocumentException, IOException, TemplateException
    {
        String basePath = request.getSession().getServletContext()  
                .getRealPath("/");  
        /* 创建配置 */  
        @SuppressWarnings("deprecation")
        Configuration cfg = new Configuration();  
        /* 指定模板存放的路径 */  
        cfg.setDirectoryForTemplateLoading(new File(basePath + "/WEB-INF/ftl/pdf"));  
        cfg.setDefaultEncoding("UTF-8");  

        /* 从上面指定的模板目录中加载对应的模板文件 */  
        Template temp = cfg.getTemplate(pdfName+".ftl");  

        /* 将生成的内容写入hello .html中 */  
        String file1 = basePath + "/WEB-INF/ftl/pdf/"+pdfName+".html";  
        File file = new File(file1);  
        if (!file.exists())  
            file.createNewFile();  

        Writer out = new BufferedWriter(new OutputStreamWriter(  
                new FileOutputStream(file), "utf-8"));  
        temp.process(root, out);  
        out.flush();  

        String url = new File(file1).toURI().toURL().toString();

        OutputStream os = response.getOutputStream();

        ITextRenderer renderer = new ITextRenderer();  
        // PDFEncryption pdfEncryption = new  
        // PDFEncryption(null,null,PdfWriter.ALLOW_PRINTING);  
        // renderer.setPDFEncryption(pdfEncryption); //只有打印权限的  
        renderer.setDocument(url);  

        // 解决中文问题  
        ITextFontResolver fontResolver = renderer.getFontResolver();  
            fontResolver.addFont(basePath + "/WEB-INF/classes/simsun.ttf",  
                    BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  
            fontResolver.addFont(basePath + "/WEB-INF/classes/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        renderer.layout();  
        renderer.createPDF(os);  
        os.close();

    }

页面、ftl共用

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>打印/导出验收单</title>
<style type="text/css">
    body {
        margin-left: 45px;
        margin-right: 45px;
        font-family: Arial Unicode MS;
        font-size: 10px;
    }
    table {
        margin: auto;
        width: 100%;
        border-collapse: collapse;
        border: 1px solid #444444;
    }
    th,td {
        border: 1px solid #444444;
        font-size: 10px;
        margin-left: 5px;
    }
    td {
        width: 150px;
    }

    @page {
        size: 12in 9in;
        margin: 0.25in;
        -fs-flow-top: "header";
        -fs-flow-left: "left";
        -fs-flow-right: "right";
    }
</style>
</head>
<body>
    <div>
        <div>
            <div>
                <div style="text-align:center;margin:0 auto 20px">
                    <span style="font-size: 20px;">${receiptName}</span>
                </div>
                <div style="font-size: 12px;">编码:${receiptId}</div>
                <table >
                    <tr> 
                        <th>物资编码</th>
                        <th>物资名称</th>
                        <th>物资型号</th>
                        <th>品牌</th>
                        <th>技术参数</th>
                        <th>验收部门名称</th>
                        <th>验收人</th>
                        <th>验收时间</th>
                        <th>订购数量</th>
                        <th>供货数量</th>
                        <th>合格数量</th>
                        <th>退回数量</th>
                    </tr>
                    ${rlShow}
                </table>
            </div>
        </div>
    </div>
</body>
</html>

主要用的jar:
freemarker
itext

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值