Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing external file on Google Chrome and Firefox using javascript

Trying to print an external HTML file using javascript. The code works in IE. However, in Google Chrome and Mozilla FireFox, it does not print the external page, it prints the current page. Any suggestions?

Header Code

<link rel="alternate" media="print" href="file.htm" />

Body Code

<a href="file.htm" onclick="window.print(); return false;">Print Agreement</a>

Google Chrome loads its "chrome://print/" for print preview, maybe causing this issue. Is there another way of doing this? So far the only workaround I can get is to open a popup and have the user print from there... But it's an extra step that seems pointless, and no one likes pop-ups anymore.

Cheers.

like image 304
UTN Avatar asked Dec 29 '11 04:12

UTN


People also ask

How do I print a JavaScript document?

JavaScript | Window print() Method. Page print in JavaScript is a simple code in JavaScript used to print the content of the web pages. The print() method prints the contents of the current window. It basically opens Print dialog box which lets you choose between various printing options.

How do you print a parameter in JavaScript?

In the above syntax, we use the window. print() method that prints the currently visible content of the window screen. Parameters: It does not contain any parameters. Returns: The window.

How do I print to PDF in Chrome?

Open a webpage in Chrome, press Ctrl+P to open the Print dialog and change the destination printer to “Save as PDF”. Hit the “Print” button and the webpage will download as a PDF document. You can also use Chrome's print-to-PDF function to convert to .


1 Answers

You should try to give users option to view first the Agreement, not directly print it. And, if you do so, you can invoke printing from the Agreement itself, with a button for example, it can be also a link, using simple HTML/JavaScript code:

<input name="print" type="button" id="print" value="Print agreement" onclick="window.print();"/>

I personally use this approach and works even with IE5.5.

like image 91
Bud Damyanov Avatar answered Oct 08 '22 13:10

Bud Damyanov