加载中…
个人资料
pigpigzhu
pigpigzhu
  • 博客等级:
  • 博客积分:0
  • 博客访问:860
  • 关注人气:5
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

java 生成pdf (页眉页脚 强制分页)

(2012-08-22 16:31:46)
标签:

杂谈

分类: java
主要使用包为itext2.0.8.jar和xhtmlrender.jar


解析下列代码:1."@page {"+
                      "size:landscape;"+
                      "margin-top:95pt;"+
                      "@top-center {content: element(header)}"+
                      "@bottom-center {content: element(footer)}"+
                    "}"+
                    ".header1 {position: running(header);color:#cccccc;font-family: SimSun;padding-top:25pt;}"+
                    ".footer { position: running(footer) }"

以上是页眉页脚的css,调用为直接加入<div class='header1' ></div> 即可;


2..pageNext{page-break-after: always;} 
这个是分页的css 只要在想分页的地方加上<div class='pageNext'></div>即可;




代码如下:

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Types;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import oracle.sql.BLOB;
import org.w3c.dom.Document;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xml.sax.SAXException;

public class PrintToPDFPSOBPD {

   
   
    public static void main(String[] args) throws Exception {       
        generatePDF(105,"http://192.168.1.119:7777/pls/apex/wwv_flow_file_mgr.get_file?p_security_group_id=3844627731404226&amp;p_flow_id=1001&amp;p_fname=Equias-Alliance-Logo-Tag-FINAL.png", "f:/ccdd.pdf");
    }
   
    //5c accrual  5d accrual   5a  5b
    public static void generatePDF(Integer propId,String ln_pic_location, String fileName) throws Exception {           
       
//连接数据库语句
//        Connection conn  = DriverManager.getConnection("jdbc:default:connection:");
        Connection conn  = DbHelper.getConnection();
       
        StringBuffer htmlString = new StringBuffer();
       
        Statement stm = conn.createStatement();
       

        //pre_flag
        String pre_flag = "";
        ResultSet rs = stm.executeQuery("select col_value from report_data where prop_id = "+propId+" and report_id = 'PSOBPD1' and report_level = 'SN' and LINE_SEQ =0 and col_name = 'pre_flag'");
        while(rs.next()){
            pre_flag = rs.getString("col_value");
        }
           
        //post_flag
        String post_flag = "";
        rs = stm.executeQuery("select col_value from report_data where prop_id = "+propId+" and report_id = 'PSOBPD1' and report_level = 'SN' and LINE_SEQ =0 and col_name = 'post_flag'");
        while(rs.next()){
            post_flag = rs.getString("col_value");
        }
           
        //count
        int par_count = 0;
        rs = stm.executeQuery("select count(prop_id) par_count from report_data where prop_id = "+propId+" and report_id = 'PSOBPD1' and report_level = 'SN' and LINE_SEQ !=0 and col_name = 'name'");
        while(rs.next()){
            par_count = rs.getInt("par_count");
        }
           
        htmlString.append("<?xml version='1.0'?><!DOCTYPE some_name [ <!ENTITY copy '&#169;'> <!ENTITY nbsp '&#160;'>]><html>");
        htmlString.append("<head><style>"+
                    "@page {"+
                      "size:landscape;"+
                      "margin-top:95pt;"+
                      "@top-center {content: element(header)}"+
                      "@bottom-center {content: element(footer)}"+
//                      "page-break-before:always"+
                    "}"+
                    ".pageNext{page-break-after: always;}   "+
                    ".header1 {position: running(header);color:#cccccc;font-family: SimSun;padding-top:25pt;}"+
                    ".footer { position: running(footer) }"+
                "body{font-size:12px;font-family:Arial,sans-serif;}"+
                "table.data{border-top: 1px solid #333;border-bottom: 1px solid #333;width:100%;border:1px solid #333;}"+
                "table{ border-collapse:collapse;}"+
                "table td{ padding:0 0 20px 0; vertical-align:top;}"+
                "table.uReportStandard > thead > tr > th{ border:0.5pt #333 solid; background:#d5d0c3;color:#000;text-align:center;font-size:15px;font-family:Arial,sans-serif;font-weight:bold;}"+
                "table.uReportStandard > tbody > tr > td{padding:1px 2px; font-size:12px;}"+
                ".data td.left_text{ font-size:12px;font-family:Arial,sans-serif;width:300px;}"+
                ".data td.right_text{ text-align:right;font-size:12px;font-family:Arial,sans-serif;width:120px;}"+
                "table#uPageCols td#uRightCol,table#uPageCols td#uRightCol aside{width:0;}"+
                "table.uReportStandard{border:0.5px #333 solid;}"+
               
            "</style></head>");
        htmlString.append("<body id='praint'>");
        htmlString.append("<div id='uOneCol'><div>");
        //页眉
        htmlString.append("<div style='width:1000px;margin:0 auto;'><div class='header1' style='text-align:right;margin-top:0;'><div style='text-align:right;margin:0;'>"+
                        "<img width='240px' src='"+ln_pic_location+"'/>"+
                    "</div><br />");   
           
        htmlString.append("<div style='text-align:center;margin-bottom:2px;font-family:Arial,sans-serif;'>"+
                    "<span style='color: #7D1931;font-size:14pt;'>"+
                        "Supplemental Executive Retirement Plan"+
                    "</span><br />"+
                    "<span style='color: #7D1931;text-decoration: underline;font-size:18pt;'>"+
                        "Participant Summary of Benefit Plan Design"+
                    "</span>"+
                "</div></div>");
           
        htmlString.append("<div style=' width:100%; margin:0 auto;'>");
        htmlString.append("<table summary='accrualschedule' class='uReport uReportStandard' style='width:100%;margin:0 auto;'>");
        htmlString.append("<thead><tr><th align='center' style='color:#000;padding:25px;'>Name</th>"+
                            "<th align='center' style='color:#000;'>Date of Birth</th>"+
                            "<th align='center' style='color:#000;'>Age as of 2012-3-1</th>"+
                            "<th align='center' style='color:#000;'>Proj. Ret. Age</th>"+
                            "<th align='center' style='color:#000;padding:10px 45px;'>Benefit Formula</th>"+
                            "<th align='center' style='color:#000;'>Initial Annual Benefit</th>");
                   
                            if (pre_flag.equals("Yes")){
                                htmlString.append("<th align='center' style='color:#000;'>Pre Ret. Inflator</th>");
                            }
                            if (post_flag.equals("Yes") ){
                                htmlString.append("<th align='center' style='color:#000;white-space: nowrap;'>Post Ret.<br/>Inflator</th>");
                            }
                            htmlString.append("<th align='center' style='color:#000;padding:10px 45px;'>Payment Duration</th>"+
                            "<th align='center' style='color:#000;'>Projected Total Payments</th>"+
                            "<th align='center' style='color:#000;'>Liability at Proj. Ret. Date</th>"+
                            "<th align='center' style='color:#000;'>Discount Rate</th></tr></thead>");
        htmlString.append("<tbody>");   
           
        for(int i = 1;i<=par_count;i++){
            rs = stm.executeQuery("select col_value,col_position from report_data where prop_id = "+propId+" and report_id = 'PSOBPD1' and report_level = 'SN' and LINE_SEQ = "+i+"  order by col_position");
            htmlString.append("<tr class='font-size-css'>");
               
            while(rs.next()){
                if(rs.getInt("col_position")==1 ){
                    htmlString.append("<td align='left' >"+rs.getString("col_value")+"</td>");
                }else if(rs.getInt("col_position")==2||rs.getInt("col_position")==3||rs.getInt("col_position")==4){
                    htmlString.append("<td align='center' >"+rs.getString("col_value")+"</td>");
                }else{
                    htmlString.append("<td align='right' >"+rs.getString("col_value")+"</td>");
                }
            }
            htmlString.append("</tr>");
        }
           
        htmlString.append("</tbody>");
        htmlString.append("</table>");
           
        htmlString.append("</div></div></div></div>");

        //分页
        htmlString.append("<div class='pageNext'></div>");
           
        //pg2_pre_flag
        String pg2_pre_flag = "";
        rs = stm.executeQuery("select col_value from report_data where prop_id = "+propId+" and report_id = 'PSOBPD2' and report_level = 'SN' and LINE_SEQ =0 and col_name = 'pg2_pre_flag'");
        while(rs.next()){
            pg2_pre_flag = rs.getString("col_value");
        }
           
        //pg2_post_flag
        String pg2_post_flag = "";
        rs = stm.executeQuery("select col_value from report_data where prop_id = "+propId+" and report_id = 'PSOBPD2' and report_level = 'SN' and LINE_SEQ =0 and col_name = 'pg2_post_flag'");
        while(rs.next()){
            pg2_post_flag = rs.getString("col_value");
        }
           
        //count
        par_count = 0;
        rs = stm.executeQuery("select count(prop_id) par_count from report_data where prop_id = "+propId+" and report_id = 'PSOBPD2' and report_level = 'SN' and LINE_SEQ !=0 and col_name = 'pg2_name'");
        while(rs.next()){
            par_count = rs.getInt("par_count");
        }
           
        htmlString.append("<div id='uOneCol'><div><div style='width:1000px;margin:6pt; auto;'>");
           
        htmlString.append("<div style=' width:100%; margin:0 auto;'>");
        htmlString.append("<table summary='accrualschedule' class='uReport uReportStandard' style='width:100%;margin:0 auto;'>");
        htmlString.append("<thead><tr><th align='center' style='color:#000;padding:25px;'>Name</th>"+
                            "<th align='center' style='color:#000'>Date of Birth</th>"+
                            "<th align='center' style='color:#000'>Age as of 2012-3-1</th>"+
                            "<th align='center' style='color:#000'>Proj. Ret. Age</th>"+
                            "<th align='center' style='color:#000'>Current Salary</th>"+
                            "<th align='center' style='color:#000'>Salary Inflator</th>"+
                            "<th align='center' style='color:#000'>Proj. Final Salary</th>"+
                            "<th align='center' style='color:#000'>Benefit Formula</th>"+
                            "<th align='center' style='color:#000'>Initial Annual Benefit</th>");
                           
                            if (pg2_post_flag.equals("Yes") ){
                                htmlString.append("<th align='center' style='color:#000;'>Post Ret. Inflator</th>");
                            }
                            htmlString.append("<th align='center' style='color:#000;padding:10px 45px;'>Payment Duration</th>"+
                            "<th align='center' style='color:#000'>Projected Total Payments</th>"+
                            "<th align='center' style='color:#000'>Liability at Proj. Ret. Date</th>"+
                            "<th align='center' style='color:#000'>Discount Rate</th></tr></thead>");
        htmlString.append("<tbody>");   
           
           
        for(int i = 1;i<=par_count;i++){
            rs = stm.executeQuery("select col_value,col_position from report_data where prop_id = "+propId+" and report_id = 'PSOBPD2' and report_level = 'SN' and LINE_SEQ = "+i+"  order by col_position");
            htmlString.append("<tr class='font-size-css'>");
           
            while(rs.next()){
                if(rs.getInt("col_position")==1 ){
                    htmlString.append("<td align='left' >"+(rs.getString("col_value")!=null ?rs.getString("col_value"):"")+"</td>");
                }else if(rs.getInt("col_position")==2||rs.getInt("col_position")==3||rs.getInt("col_position")==4){
                    htmlString.append("<td align='center' >"+(rs.getString("col_value")!=null ?rs.getString("col_value"):"")+"</td>");
                }else{
                    htmlString.append("<td align='right' >"+(rs.getString("col_value")!=null ?rs.getString("col_value"):"")+"</td>");
                }
            }
            htmlString.append("</tr>");
        }
           
            htmlString.append("</tbody>");
            htmlString.append("</table>");
               
        htmlString.append("</div></div></div></div>");

        htmlString.append("</body>");
        htmlString.append("</html>");  
       

         //此处为将html转换为document,然后用itextRender渲染生成pdf
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = builder.parse(new ByteArrayInputStream(htmlString.toString().getBytes()));
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(doc, null);
        renderer.layout();
       
        // write to the output stream
//        BLOB blob = BLOB.createTemporary(conn, true, BLOB.DURATION_SESSION);
//        OutputStream out = blob.setBinaryStream(1l);
//        renderer.createPDF(out);
//        out.close();
//        return blob;
       

        OutputStream out = new FileOutputStream(fileName);
        renderer.createPDF(out);
        out.close();
        System.out.println("ok");
    }
   
}








0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有