Convert URL to PDF And Print Using JavaScript

To do Convert External URL to PDF And Print Using JavaScript, we can use hidden <iframe>.

Here we do a complete code example, where we will see how we can print an external URL as a PDF without opening it on the same page.

At the web development sometimes we have to show some pages as PDF or download HTML pages as PDF via URL using JS.

Convert URL to PDF And Print Using JavaScript

Also Read: Download HTML Page as PDF Using JavaScript

This is the important concept of the conversion of URL to PDF by using JavaScript.

Here is the complete source code to Convert External URL to PDF And Print Using JavaScript,

Code Highlights:

  1. Here we create 3 functions, first is for print windows close operation, second is for eventes work on print window
  2. And last function (printPage(sURL)) is for create hidden iframe which used to print or we can save it as PDF.
  3. On printPage(sURL) function we can define the style of the print PDF file.
  4. By using the same function we can manage height, width, border and alignment of the generated PDF for print.
  5. At last we had create a span (you can choose the button) where we call onclick() function to call the print function with URL as argument.

How to Use

Copy the complete source code and paste it into your project.

Use onclick function to call the complete print PDF function with a button or text click.


Sometimes you get the CORS error which occurs because of different domains, so you have to first set the CORS permission on that URL which you want to print.

It is also helpful when you want to print a PDF file using JavaScript without opening it and print directly to the default printer.

Also Read: How to Embed PDF in Website Using HTML

I hope you get a complete understand of the code.

Please let me know if you are facing any issues with the implementation.

Happy Coding..!

Was this article helpful?
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.
Posts created 188

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top