使用iText插件将HTML文件转化为PDF(支持图片相对地址)

String fileName = "项目可行性研究报告";

        response.setContentType("application/pdf");

        response.setHeader("Content-Disposition", "attachment; filename=\""
                + new String(fileName.getBytes("gb18030"), "ISO8859-1") + ".pdf" + "\"");



        String url = request.getRequestURL();
        String url1 = url.substring(0,28);

        ProjectFeasibility   projectFeasibilityList = ProjectFeasibility.get(Integer.parseInt(params.id));


            //  String htmlFile = projectFeasibilityList.get(i).projectOverview;

            StringBuffer html = new StringBuffer();
            // DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
        html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
        html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">").
                 append("<head>")
                .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
                .append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>")
                .append("</head>")
                .append("<body>");

        html.append("<strong><h2>研发经费来源及预算</h2></strong>")
        if(projectFeasibilityList.fundSources==null||projectFeasibilityList.fundSources==""||projectFeasibilityList.fundSources=="null"||projectFeasibilityList.payBudget==null||projectFeasibilityList.payBudget==""||projectFeasibilityList.payBudget=="null"){
            html.append("");
        }else{

            ProjectFundSources projectFundSources = projectFeasibilityList.fundSources;

            if(null==projectFundSources.loan){
               projectFundSources.setLoan(0)
            }else if(null==projectFundSources.ownExpenses){
                projectFundSources.setOwnExpenses(0)
            }

            PayBudget payBudget = projectFeasibilityList.payBudget;



            if(null==payBudget.labour){
               payBudget.setLabour(0)
            }
            if(null==payBudget.directInvestment){
                payBudget.setDirectInvestment(0)
            }
            if(null==payBudget.depreciation){
                payBudget.setDepreciation(0)
            }
            if(null==payBudget.devise){
                payBudget.setDevise(0)

            }
            if(null==payBudget.testingEquipment){
                payBudget.setTestingEquipment(0)
            }
            if(null==payBudget.assets){
                payBudget.setAssets(0)
            }
            if(null==payBudget.entrust){
                payBudget.setEntrust(0)
            }
            if(null==payBudget.otherExpenses){

                payBudget.setOtherExpenses(0)
            }



            html.append("<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border:solid 1px #000000;width:680px;height:840px;\">\n" +
                    "  <tr>\n" +
                    "    <td colspan=\"4\" height=\"70px\"><div style=\"float:left\">&nbsp;&nbsp;&nbsp;&nbsp;项目的研发费用预算</div><div style=\"float:right;margin-right:10px;\">单位:万元</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td colspan=\"2\" height=\"70px\"><div align=\"center\">经费来源预算</div></td>\n" +
                    "    <td colspan=\"2\" height=\"70px\"><div align=\"center\">经费支出预算</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">科  目</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">预算数</div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">科  目</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">预算数</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">项目投资合计</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+(projectFundSources.ownExpenses+projectFundSources.loan)+"</div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">支出预算合计</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+(payBudget.labour+payBudget.directInvestment+payBudget.depreciation+payBudget.devise+payBudget.testingEquipment+payBudget.assets+payBudget.entrust+payBudget.otherExpenses)+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">一、单位自筹</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+projectFundSources.ownExpenses+"</div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">一、人员人工</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.labour+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">二、商请银行贷款</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+projectFundSources.loan+"</div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">二、直接投入</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.directInvestment+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">三、设备折旧费与长期费用摊销</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.depreciation+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">四、设计费</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.devise+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">五、科技设备调试费</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.testingEquipment+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">六、无形资产摊销</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.assets+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">七、委托外部开发费</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.entrust+"</div></td>\n" +
                    "  </tr>\n" +
                    "  <tr>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\"></div></td>\n" +
                    "    <td width=\"30%\" height=\"70px\"><div align=\"center\">八、其他费用</div></td>\n" +
                    "    <td width=\"20%\" height=\"70px\"><div align=\"center\">"+payBudget.otherExpenses+"</div></td>\n" +
                    "  </tr>\n" +
                    "</table>");

        }

        html.append("<br/><br/><br/><br/><br/><br/>")

html.append("<strong><h2>一、项目概述</h2></strong>")
            if(projectFeasibilityList.projectOverview==null||projectFeasibilityList.projectOverview==""||projectFeasibilityList.projectOverview=="null"){
                html.append("");
            }else{
                html.append(projectFeasibilityList.projectOverview);
            }


。。。略去类似代码。。。



html.append("<strong><h2>九、所属项目</h2></strong>")

            html.append(projectFeasibilityList.project.name);
            html.append("<br/><br/><br/>")
            html.append("</body></html>");
            ITextRenderer renderer = new ITextRenderer();
            // 解决中文支持问题
            ITextFontResolver fontResolver = renderer.getFontResolver();
            fontResolver.addFont("C:/Windows/Fonts/simsun.ttc", com.itextpdf.text.pdf.BaseFont.IDENTITY_H, com.itextpdf.text.pdf.BaseFont.NOT_EMBEDDED);
            renderer.setDocumentFromString(html.toString());
            renderer.getSharedContext().setBaseURL(url1);                    //解决图片相对路径问题
            renderer.layout();
            renderer.createPDF(response.getOutputStream());
            response.outputStream.flush();
            response.outputStream.close();



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值