简体   繁体   English

如何使用javascript打印本地pdf文件

[英]How to print local pdf file using javascript

For example, I having local pdf file which contains 6 pages. 例如,我有包含6页的本地pdf文件。 If I using window.print() function, In print preview will shown only one page what ever in the browser. 如果我使用window.print()函数,则在打印预览中将仅显示浏览器中的一页。 Instead of single page I have to show all the pages in print preview mode. 我必须以打印预览模式显示所有页面,而不是单页面。

Bind all the required pages or data around a div and print a div instead and for printing a div you just have to place this much of code. 将所有必需的页面或数据绑定到一个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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM