Home Web DevelopmentHTML How to Convert HTML to PDF

How to Convert HTML to PDF

HTML is an acronym for Hypertext Markup Language. It’s used for designing static web pages. Html is normally displayed in web browsers. PDF is an acronym for Portable Document Format. It’s easy to read and share since it’s in a printed, electronic format.

Benefits of converting HTML to PDF

  • PDF documents are secure since they can be protected with a password, unlike HTML pages that are accessible to everyone on the web
  • PDF documents can be easily shared and also can easily be created.
  • With pdf, it is possible to view and read offline.
  • PDF documents can be compressed to save space on your computer and only used when needed.

There are different numerous ways in which you can convert HTML to PDF. Some include online conversion, using software’s and also through running some lines of codes using libraries. Depending on your preferences, you can determine the most and the best method for you to use.

Lets have a look some online conversion methods:

Online conversion of HTML to PDF

Win2PDF

This provides free online conversion of public HTML webpages to PDF. You enter the address of the HTML page you want to convert to PDF and click submit. With this method, it’s not possible to create PDFs from local files on your computer. Secondly, it’s not possible for security reasons to create PDFs from HTML pages that have a login—such as an email box, online banking, payment checkout, or shopping cart content.
You can try Win2PDF here.

You can also convert files by printing to win2PDF using a windows browser like Chrome or Firefox. Press CTRL+P on the HTML page you want to print. On the pop-up window, choose your printer. In our case, select Win2PDF and print.

With Win2PDF, you can also convert your local files such as word to PDF with their free trial. You can download it from here. The only disadvantage with this method is that It has a watermark unless you purchase the full version.

Online- convert

You only need to upload your files and start the conversion. With this page, you can enter the URL, choose files from google drive or upload from your computer. It has an option to download as a zip file.
You can try it here: online-convertor.

Adobe acrobat dc

It’s also an online way that offers a 7 days free trial only. It’s easy and direct to use.
Try it here; Adobe acrobat dc

Chrome web store extension

Chrome extension is another way to convert HTML to PDF. Download the extension from here. To convert, click on the extension icon. It will open HTML to PDF online conversion websites. Select the HTML file you want to convert and click convert. Also, don’t forget to enter the email address where the document download link will be sent.

Other Online Services:

These are available for direct use:

Software Conversion of HTML to PDF

Evo PDF software

It uses a converter library for .NET and .NET core. The best advantage of converting HTML to PDF using Evo PDF software is that it allows full support conversion. All HTML tags, SVG vector graphics, CSS styles, live URLs, and bookmarks are interpreted and converted as they appear on the page. It also includes the header and footers in the PDF converted. It also allows you to choose the PDF page options.
You can download it from here: Evo PDF Software.

Using Libraries to convert HTML to PDF

We shall use wkhtmltopdf python library to convert HTML to PDF.

Step 1: First install the client library pdfcrowd by typing this command:

$ pip install pdfcrowd
pdfcrowd

Step 2: Download the pdfkit library.

$ pip install pdfkit
pdfkit

Step 3: Download the wkhtmltopdf library
In any Linux platform such as Ubuntu and Debian type this command:

sudo apt-get install wkhtmltopdf

In Windows, you can download the setup here: wkhtmltopdf
Set the path: To do this, navigate to this directory “C:\Program Files\wkhtmltopdf\bin” and copy the path.
Go to windows advanced system settings, then environment variables, add the new path, and click OK.

Path

Step 4: Now, all libraries are set to use. With this method, you can convert a single file saved in .HTML or using a URL.

import pdfkit
//for converting files
pdfkit.from_file("index.html", "mypdf.pdf")
//for converting urls
pdfkit.from_url("https://www.codeunderscored.com/","codeunderscored.pdf")

You first have to import the pdfkit library to make it usable. In the two strings enclosed by curl brackets, the first string represents your file or the URL you need to convert, while the second string holds the name of your output pdf file.

Let’s check an example by converting Code Underscored webpage into HTML.

Input:

import pdfkit
pdfkit.from_url("https://www.codeunderscored.com/","codeunderscored.pdf")

Output:

Conclusion

In this tutorial, we covered some of the various ways to convert HTML to PDF—the best advantage of converting an HTML page to pdf maybe for portability and easy access when offline. There are more various ways to achieve this. In our case, we have studied online conversion, software conversion, and wkhtmltopdf python library.

You may also like

2 comments

Kennedy January 13, 2021 - 4:31 am

Really nice and educative article. Helped me a lot.

Reply
Samuel Ouma January 13, 2021 - 5:40 am

Very good Moses. Easy to follow.

Reply

Leave a Reply to Kennedy Cancel Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More