Print PDF in client automatically

Hi,this is Arren.

    Maybe you will try to print PDF files which generated on server by your application.
And you want to transfer them to client for printing. Print automatically in browser when you embed the file in a html page with ActiveX controls support maybe archive your desires. But it can't run in linux os. Maybe you will select using java applet to do this printing, but pure java doesn't support printing PDFs, and you will need to write many code to implement this simple task. The free Adobe reader is installed on many PCs aroud the world. And it provides perfect printing for PDF files.
    IN my recent project, I generate PDF files in http server using iText library, and then I need this file embed in a html, when it loaded completely, it should start print silently; When finished printing, It should notify the user that it finished. By this, the user needn't to press the "Print" button in the adobe reader, but we got full controls to the printing process.

    [1] Generate PDFs on server using iText lib, or just a file.:
Document doc  =   new  Document(); 
PdfWriter writer 
=  PdfWriter.getInstance(doc, oByteArrayOutputStream); 
doc.open(); 
//  fill document content with your content. 
// .... 
//  Before we close the PDF, we add one PdfAction to the document. 
StringBuilder sb = new  StringBuilder(); 
sb.append(
" var _this=this;  "
.append(
" this.hostContainer.messageHandler={ "
.append(
"  onMessage:function(aMessage){ _this.print(false); _this.hostContainer.postMessage(['ok']); }, "
.append(
"  onError:function(error, aMessage){},  "
.append(
"  onDisclose:function(cURL, cDocURL){return true;} " )
.append(
" }; " ); 
writer .addJavaScript(sb.toString()); 

    [2] HTML file embed the PDF:
<% @page contentType = " text/html " %>
<% @page pageEncoding = " UTF-8 " %>
<%--  本页显示打印对话框,
     
" AzPrinter " 将负责生成要打印的文件,
     在本页中开始打印此文档。
         @Arren zhang
--%>
< html >
    
< head >
        
< meta http - equiv = " Content-Type "  content = " text/html; charset=UTF-8 " >
        
< title > Print Startup Interface </ title >
< script language = " javascript " >
<!--
    var read_file_timer;
    function stopFileCheck()
{
         
if(read_file_timer){
             clearTimeout(read_file_timer);
             read_file_timer
=null;
         }

     }


     
/** start load file 
      **
*/

     function init()
{        
        var oHandler
={
            onMessage:function(aMessage)
{
                
if(aMessage[0]=="file ok"){
                    stopFileCheck();
                    window.parent.AzPrinter.handlePrintStatus(
999);
                    
this.container.postMessage(["print"]);
                }
else if(aMessage[0]=="print ok"){
                    window.parent.AzPrinter.handlePrintResult(
0);
                }
else{
                    var s
=aMessage[0];
                    
if(s=="no data") s="无法生成打印数据";
                    window.parent.AzPrinter.handlePrintResult(
10, s);
                }

            }
,
            onError:function(error, aMessage)
{
                stopFileCheck();
                window.parent.AzPrinter.handlePrintResult(
11""+error);
            }

        }
;
        var obj 
= document.object_pdf;        
        
if(typeof obj.postMessage!="function"){
            window.parent.AzPrinter.handlePrintResult(
2"不支持打印");
        }
else{
            
/** 如果10秒内pdf文件没有传递给浏览器,我们就认为再也回不来了。报错
             *
*/

            read_file_timer
=setTimeout(function(){
                stopFileCheck();
                window.parent.AzPrinter.handlePrintResult(
3," 无法生成打印数据");
                document.close();
            }
10*1000);

            oHandler.container 
= obj;
            obj.messageHandler 
= oHandler;
            obj.postMessage([
"check file"]); //dump message
        }

     }


     function printerError()
{
        window.parent.AzPrinter.handlePrintResult(
4,"浏览器错误");
     }

// -->
</ script >
    
</ head >
    
< body onerror = " printerError(); "  onload = " init(); " >
        
< object id = " object_pdf "  name = " object_pdf "  type = " application/pdf "  
                data
= " /rentsys/_check/AzPrinter?<%=request.getQueryString()%> "  
                style
= " position:absolute; top:0px; left:0px; visibility:hidden "  width = " 1 "  height = " 1 "  border = " 0 " >
        
</ object >
    
</ body >
</ html >


 If you are writing a program to print invoice in PDF format, I hope this can help you.
For more detailed code, you can email me on arrenzhang@gmail.com.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值