2016-04-01 33 views
0

例如,我有包含6頁的本地pdf文件。如果我使用window.print()函數,則在打印預覽中將只顯示瀏覽器中的任何一頁。而不是單個頁面,我必須以打印預覽模式顯示所有頁面。如何使用javascript打印本地pdf文件

+0

朋友歡迎任何答案! –

回答

0

綁定div所需的所有頁面或數據,然後打印div,打印div只需放置大量代碼即可。

<html> 
<head> 
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.1.min.js" > </script> 
<script type="text/javascript"> 

    function PrintElem(elem) 
    { 
     Popup($(elem).html()); 
    } 

    function Popup(data) 
    { 
     var mywindow = window.open('', 'my div', 'height=400,width=600'); 
     mywindow.document.write('<html><head><title>my div</title>'); 
     /*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />'); 
     mywindow.document.write('</head><body >'); 
     mywindow.document.write(data); 
     mywindow.document.write('</body></html>'); 

     mywindow.document.close(); // necessary for IE >= 10 
     mywindow.focus(); // necessary for IE >= 10 

     mywindow.print(); 
     mywindow.close(); 

     return true; 
    } 

</script> 
</head> 
<body> 

<div id="mydiv"> 
    This will be printed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque a quam at nibh adipiscing interdum. Nulla vitae accumsan ante. 
</div> 

<div> 
    This will not be printed. 
</div> 

<div id="anotherdiv"> 
    Nor will this. 
</div> 

<input type="button" value="Print Div" onclick="PrintElem('#mydiv')" /> 

</body> 
</html>