Being a web developer, many times you need to allow the user to print or save HTML page as PDF.
Suppose you have a web form on your website and the user has to submit the data. After submitting the form, you can allow the user to print or save the HTML page as a PDF with all the user data. A user can use this data for future reference.
Another example. If you have a tutorial on your website, you can allow a user to print the tutorial so that the user can use it offline.
A simple solution to allow the user to print or save the HTML page as PDF will be very helpful. You don’t need any external tool to convert HTML to PDF.
Hope you are familiar with basic HTML tags. Call the Javascript function inside the HTML to print and save the webpage as PDF.
<!DOCTYPE html> <html> <body> <h2>Tutorial to print HTML page</h2> <p>Click the button to print this page.</p> <button onclick="printFunction()">Print</button> <script> function printFunction() { window.print(); } </script> </body> </html>
Here,
window.print()
is the inbuilt function to print the current window.printFunction()
is user define javascript function which you call from HTML object.By clicking on the button, the user can save the HTML page as PDF or can download it.
The print()
function supports almost all the popular web browsers like Google Chrome, Firefox, Internet Explorer, Opera, and Safari (Mac and other iOS web browsers).
If you are a web developer or interested in web development, do check how the 20 best web form design practices have helped me grow professionally.
Javascript is one of the highest-paying programming languages and has very advanced programming features.
This is a simple JavaScript function to convert HTML to PDF or to print or save HTML page as PDF. You can run this code in an online HTML editor for testing.
For more of such content, stay tuned.
I want to print a specific part of the page and not the entire page. Please suggest.
Set the div for the content you want to print. And then use the
write()
method inside the JavaScript function.Let’s say…
Try this and let me know if this is working for you.
Thanx, but its redirect to new tab…
This print functions works! But how to use it as Save as a function?
When you click on the print button, you can print the page if you are connected to the printer or you can also save the page as PDF.
Hi
Is there any other way to code for saving as pdf, rather than doing it manually?
Means in one click, it automatically selects the ‘window. print’ and select ‘save as PDF’ and saving the PDF
Thanks in advance.
Hi,
In that case, you have to create the PDF file on the server and then allow the user to download it. Which server-side programming you are using?
Hi
Is there any way to ‘save as pdf ‘ programmatically using
window.print()
? I’ve used libraries for it. But the CSS became the issue there.Thanks in advance.
window.print()
works with JavaScript and it gets executed in the browser. Which programming language solution you are looking for?Simple and very useful trick.
thank you very much for the tip!
You’re welcome.
Good evening sir
Code to print the upload file. For example, I am uploading files means, I want to print the same file coding in HTML based using javascript.
Yeah, you can do that. Provide media upload in the form. Once the document is uploaded by the user, you can display PDF file.
Hi
Is there any way to ‘save as pdf ‘ programmatically using window.print()? I am using Python.
Hi Rutuja,
In this tutiorial, We are already using
window.print()
.Hi, Anyway I can save as pdf direct without click button print in popup ?
Thankyou!
I think you are asking to print the page without a print button on it. If so, you can print specific div as well. So you can skip printing the complete page and print button.
Hi,
Do you know how can I print the letters: ű,ő,ú and so one.
I’m Hungarian.
Thank you!!
You have to use unicode formatting as these are special characters.
console.log(‘Hungarian characters: ű, ő, ú’);