Skip to content
Home » Convert URL to PDF And Print Using JavaScript

Convert URL to PDF And Print Using JavaScript

To Convert 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.

In 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, the first is for print window close operation, and the second is for events work on the print window
  2. And the last function (printPage(sURL)) is for creating a hidden iframe which is used to print or we can save it as a PDF.
  3. On printPage(sURL) the function we can define the style of the print PDF file.
  4. By using the same function we can manage the height, width, border, and alignment of the generated PDF for print.
  5. At last, we created a span (you can choose the button) where we call onclick() function to call the print function with URL as an 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.

Note:

Sometimes you get the CORS error which occurs because of different domains, so you have to first set the CORS permission on the URL that 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 understanding of the code.

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

Happy Coding..!

Was this article helpful?
YesNo

Leave a Reply

Your email address will not be published. Required fields are marked *