使用window.print打印外部HTML


$.get("http://example.com/invoice.html", function( data ) {
                console.log(data); //work
                window.print(data); // doesn't
            });

如何正确加载外部html并打印它?上面的代码不起作用,

它打印网页本身,而不是我想要的外部html。

任何想法?

Print函数不接受任何参数,它只打印窗口数据。

只要你的数据是html(要正确格式化和查看),你可以打开一个新窗口,在那里你设置你的数据,关闭它,并使用打印功能,因为对象仍然是可用的,你也可以链接任何CSS特定的打印数据在新窗口:

$.get("http://example.com/invoice.html", function( data ) {
       var printWindow = window.open('', '', 'height=400,width=800');
       printWindow.document.write('<html><head><title>DIV Contents</title>');
       printWindow.document.write('</head><body >');
       printWindow.document.write(data);
       printWindow.document.write('</body></html>');
       printWindow.document.close();
       printWindow.print();
});

window.print()不接受任何参数。

你需要打开得到的HTML在一个新的窗口,或加载到一个模态覆盖,然后调用window.print .

最新更新