`
八贤王
  • 浏览: 38579 次
社区版块
存档分类
最新评论

页面生成pdf

    博客分类:
  • JAVA
阅读更多
面生成pdf说明文档
一、依赖的jar包
fonts.jar、pd4ml.jar、ss_css2.jar
二、功能说明
该功能可将jsp页面,直接转换成pdf文件。不需要做任何其他的配置,只需要前期导入依赖的jar包,然后传入函数需要的url(jsp页面action的地址)和pdf文件名称(包括路径),就可以生成需要的pdf到对应的路径下。
三、使用说明
(1) 将3个依赖的jar包导入工程;
(2) 系统代码
ConvertPdf.class-----基础类
public class ConverterPdf {
    // 手动构造HTML代码
    public static void generatePDF_1(File outputPDFFile, StringReader strReader) throws Exception {
        FileOutputStream fos = new FileOutputStream(outputPDFFile);
        PD4ML pd4ml = new PD4ML();
        pd4ml.setPageInsets(new Insets(10, 20, 10, 20));
        pd4ml.setHtmlWidth(950);
        //pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
        pd4ml.setPageSize(PD4Constants.A4);
        pd4ml.useTTF("java:fonts", true);
        pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New");
        pd4ml.enableDebugInfo();
        pd4ml.render(strReader, fos);
    }

    // HTML代码来自于HTML文件
    public static void generatePDF_2(File outputPDFFile, String inputHTMLFileName) throws Exception {
        FileOutputStream fos = new FileOutputStream(outputPDFFile);
        PD4ML pd4ml = new PD4ML();
        pd4ml.setPageInsets(new Insets(5, 20, 20, 20));
        pd4ml.setHtmlWidth(1000);
        //pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
        pd4ml.setPageSize(PD4Constants.A4);
        pd4ml.useTTF("java:fonts", true);
        pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New");
        pd4ml.enableDebugInfo();
        pd4ml.render("file:" + inputHTMLFileName, fos);
    }
}

调用页面的源码:
/**
*
*/
public String getAccountPdfHtml(String aid){
HttpServletRequest request = Struts2Utils.getRequest();
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String url = basePath+"user/guser!accountPdf.action";
//action的参数
NameValuePair[] paras = new NameValuePair[1];
paras[0] = new NameValuePair("aid", aid);  
String result = PostHttp.postHttpReturnDoc1(url, paras,null);
return result;
}

public String accountPdf(){
corp = this.corpManager.getCorpByAid(aid);
List<CrUser> users = this.userManager.findCrUserPageByAid(aid, 1, 10).getResult();
Struts2Utils.getRequest().setAttribute("orgInfo", corp);
Struts2Utils.getRequest().setAttribute("orgUserList", users);
return "accountPdf";
}

private String createPdf(String aid ,boolean isSendEmail) {
String html = this.getAccountPdfHtml(aid);
StringReader strReader = new StringReader(html);
String rootPaht = getRootPath();
new FileUtil().createFolder(rootPaht + "/pdfFiles");
new FileUtil().createFolder(rootPaht + "/pdfFiles/" + aid);
String filePath = rootPaht + "/pdfFiles/" + aid + "//账户开立申请表.pdf";
try {
ConverterPdf.generatePDF_1(new File(filePath), strReader);
if(isSendEmail){
this.setAid(aid);
this.sendEmail();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
分享到:
评论
1 楼 C203032S 2015-03-12  
感觉action生成pdf的代码不完全

相关推荐

Global site tag (gtag.js) - Google Analytics