动态生成的html页面转pdf并且打印预览

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1968640005/article/details/74938790

文章说明

程序的功能:

       通过js动态生成html页面并将该页面传送到后台转成pdf,再通过前台将pdf打印预览

先大约讲述一下具体实现过程

1、在js页面将你自己需要拼装好html页面通过ajax传到后台

2、通过itext将html页面转成pdf

3、js将pdf打印预览


具体代码

代码下载地址http://download.csdn.net/detail/a1968640005/9894741

1、js 拼接html页面并且将html传后台的代码

var printHtml = "<?xml version='1.0' encoding='UTF-8'?>";
printHtml += "<html xmlns='http://www.w3.org/1999/xhtml'>";
printHtml +="<head>";
printHtml +="<title>页面打印</title>";
printHtml +="<style media= 'print' >";
printHtml +=" @page{size:297mm 210mm;};";
printHtml +=" .page { page-break-after: always; } </style> ";
printHtml +=" <style type='text/css'>";
printHtml +=" .tab td {  border-bottom: 1 solid #000000;  border-left: 1 solid #000000;  border-right: 0 solid #ffffff;  border-top: 0 solid #ffffff;  } ";
printHtml +=" .tab {  border-color: #000000 #000000 #000000 #000000;  border-style: solid;  border-top-width: 2px;  border-right-width: 2px;   border-bottom-width: 1px;  border-left-width: 1px;  }  ";
printHtml +="  .hr {  font-family: '宋体';  font-size: 9pt;  } ";
printHtml +="  </style> ";
printHtml +=" </head> ";
printHtml +="  <body bgcolor='white' style='font-family:SimSun; height:100%;' screen_capture_injected='true' ryt11773='1'>  ";
printHtml +=" <table cellspacing='0' cellpadding='0' width='100%' align='center' > ";
printHtml +=" <tr>  <td align='center' colspan='2' style='font-size: 24px'>  <b id ='pdf_text'>深圳农村商业银行活期对账单</b>  </td>  ";
printHtml +="   <td align='right'  style='font-size: 14px'>    第1页, 共2页  </td>  </tr> ";
printHtml +="  <tr>  <td align='left' >子账户:600117332664</td> <td align='left'  >户名:王树</td> <td align='center'  >起始日期:20170607</td>  </tr>  ";
printHtml +=" <tr> <td align='left' colspan='2' >开户行:深圳农商行 </td> <td align='center' >终止日期:20170607 </td> </tr> ";
printHtml +="  <tr> ";
printHtml +="  <td colspan='3'>";
printHtml +=" <table class='tab' cellSpacing='0' cellPadding='0' width='100%' border='1' > ";
printHtml +=" <tr align='center' height='23'>  <td width='10%' height='23' colspan='3'>子账户:000117332670</td> <td width='10%' colspan='4' >币种:000117332670</td> <td width='10%' colspan='4' >产品类型:个人人民币</td>   </tr> ";
printHtml +="  <tr align='center' height='23'>  <td width='5%' height='23'>交易日期</td>  <td width='5%'>摘要</td>  <td width='12%'>借方交易金额</td>  <td width='12%'>贷方交易金额</td>   <td width='12%'>账户余额</td>";  
printHtml +=" <td width='12%'>对方户名</td>  <td width='12%'>对方行号</td>  <td width='7%'>凭证类型</td>  <td width='8%'>凭证号码</td>  <td width='10%'>交易机构</td>  <td width='10%'>备注</td> </tr> ";
for(var i = 0 ;i<= 20;i++){
printHtml +=" <tr style='font-size: 13px' align='center' height='23'>  <td align='center'>20160101</td>  <td align='center'>自助汇款</td>  <td  align='right' id ='pdf_text2' > 22.00</td>  <td align='right' >213123123.00</td>   <td align='right'>490000000.00</td>  ";
printHtml +=" <td align='center'>足足</td>  <td align='center' >中国工商银行</td>  <td align='center'>存折</td>  <td align='center'>1147755</td>  <td align='center'>00003</td> <td align='center'>拿去花</td> </tr>  ";
}
printHtml +="    </table>   </td>  </tr>  </table>  ";

printHtml +=" <div class='page' style = 'page-break-after: always'></div>";

var printHtml2 =" <table cellspacing='0' cellpadding='0' width='100%' align='center' > ";
printHtml2 +=" <tr>  <td align='center' colspan='2' style='font-size: 24px'>  <b id ='pdf_text'>深圳农村商业银行活期对账单</b>  </td>  ";
printHtml2 +="  <td align='right'  style='font-size: 14px'>    第2页, 共2页  </td>  </tr> ";
printHtml2 +="  <tr>  <td align='left' >子账户:600117332664</td> <td align='left'  >户名:王树</td> <td align='center'  >起始日期:20170607</td>  </tr>  ";
printHtml2 +=" <tr> <td align='left' colspan='2' >开户行:深圳农商行 </td> <td align='center' >终止日期:20170607 </td> </tr> ";
printHtml2 +="  <tr> ";
printHtml2 +="  <td colspan='3'>";
printHtml2 +=" <table class='tab' cellSpacing='0' cellPadding='0' width='100%' border='1' > ";
printHtml2 +=" <tr align='center' height='23'>  <td width='10%' height='23' colspan='3'>子账户:000117332670</td> <td width='10%' colspan='4' >币种:000117332670</td> <td width='10%' colspan='4' >产品类型:个人人民币</td>   </tr> ";
printHtml2 +="  <tr align='center' height='23'>  <td width='5%' height='23'>交易日期</td>  <td width='5%'>摘要</td>  <td width='12%'>借方交易金额</td>  <td width='12%'>贷方交易金额</td>   <td width='12%'>账户余额</td>";  
printHtml2 +=" <td width='12%'>对方户名</td>  <td width='12%'>对方行号</td>  <td width='7%'>凭证类型</td>  <td width='8%'>凭证号码</td>  <td width='10%'>交易机构</td>  <td width='10%'>备注</td> </tr> ";
for(var i = 0 ;i <= 20;i++){
printHtml2 +=" <tr style='font-size: 13px' align='center' height='23'>  <td align='center'>20160101</td>  <td align='center'>自助汇款</td>  <td  align='right' id ='pdf_text2' > 22.00</td>  <td align='right' >213123123.00</td>   <td align='right'>490000000.00</td>  ";
printHtml2 +=" <td align='center'>足足</td>  <td align='center' >中国工商银行</td>  <td align='center'>存折</td>  <td align='center'>1147755</td>  <td align='center'>00003</td> <td align='center'>拿去花</td> </tr>  ";
}
printHtml2 +="    </table>   </td>  </tr>  </table>  ";
     
printHtml += printHtml2 ;

printHtml +=" </body>";
printHtml +="</html>";
       
$.ajax({  
        type:"POST",  
        url:"/ArchivesManagementSystem/userAction!checkLogin.action",  
        dataType:"json",  
        data:{"printHtml":printHtml,"printHtml2":printHtml2},  
        success:function(data){
        print2();
        }
        });

     

2、html转pdf代码

该功能需要3个包 core-renderer 、iText-2.0.8、itextpdf-5.4.5
   //action 层
   public String checkLogin() {
try{
System.out.println("开始转换pdf");
PdfUtil.htmlToPdf("C:/tomacat/webapps/ArchivesManagementSystem/print/print.pdf",printHtml);
return SUCCESS;
}catch (Exception e) {
System.out.println(e.getMessage());
return fail;
}
}


//导入的包
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;



import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.Sides;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;


import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;




import com.lowagie.text.pdf.BaseFont;


import org.w3c.dom.Document;


/**
* 把URL转换为PDF

* @param outputFile
*            , 示例:/data/fs/inspector/BJ20150522001.pdf
* @param url
*            ,示例:http :xxxx
* @return
* @throws Exception
*/
public static boolean htmlToPdf(String outputFile, String url)throws Exception {
File outFile = new File(outputFile);
if (!outFile.exists()) {
outFile.getParentFile().mkdirs();
}


OutputStream os = new FileOutputStream(outputFile);

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(url.getBytes("UTF-8")));

//Document document = new Document(PageSize.A4, 50, 50, 50, 50);
ITextRenderer renderer = new ITextRenderer();

renderer.setDocument(doc, null);

//renderer.setDocument(url);
String fontPath = PdfUtil.class.getClassLoader().getResource("").toString().replaceAll("file:/", "")+ "simsun.ttc";
System.out.println(fontPath);


// 解决中文支持问题
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont(fontPath, BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();


System.out.println("文件转换成功");
return true;
}




3、将pdf打印预览

在打印的html页面上需要  推荐博文  http://www.cnblogs.com/imfjj/p/5946289.html
添加   <iframe style="display:none" name = 'printIframe2' id="printIframe" src="/Print/print/print.pdf"></iframe>

js代码
function print2(){ 
          //添加ifrme标签
$("#addiframe").html("<iframe style='display:none' name = 'printIframe2' id='printIframe' src='/ArchivesManagementSystem/print/print.pdf'>");
        //打印预览
$("#printIframe")[0].contentWindow.print();


4、出现问题

1、设置页面大小
2、中文汉字显示问题
推荐博文 http://www.cnblogs.com/reese-blogs/p/5546806.html


效果图




展开阅读全文

没有更多推荐了,返回首页