How to Convert a Webpage to PDF in Linux

Saving a webpage as a PDF comes in handy when you archive certain resources or use them for educational purposes.

But, how do you convert a webpage to PDF in Linux?

You can either choose to go the easy way out using the web browser (GUI) on every Linux distribution or use the terminal to turn a webpage into a PDF file.

Here, I shall mention both the methods to help you get the job done.

Method 1: Converting a Webpage to PDF Using Web Browser

Even though I use Mozilla Firefox for this tutorial, you can do the same thing with any of the best browsers available for Linux.

mozilla save webpage pdf

1. Load up the webpage you want to convert.

2. Head to the browser menu to find the “Print” option or use the keyboard shortcut “Ctrl + P.”

3. By default, it should let you save it as a PDF. You need to hit “Save,” and choose the destination and save the webpage.

Save webpage as PDF in Mozilla Firefox
Save webpage as PDF in Mozilla Firefox

The one major problem with this simple approach is that it includes all the elements on the page. This may include comments, footer etc. You may use a PDF editor to remove parts of it but that’s an additional task.

A better option is to utilize a browser extension like Print Friendly. It allows you to edit and remove parts of the webpage before downloading the PDF.

Method 2: Converting a Webpage to PDF or Images Using the Terminal

You probably already know that you can browse internet in Linux terminal and even download files using the command line. That’s not surprising considering you can do a lot more in the terminal, including downloading a webpage as PDF.

A nifty open-source command-line tools wkhtmltopdf and wkhtmltoimage come to the rescue that lets you convert any HTML webpage to a PDF or image file.

It utilizes the Qt WebKit rendering engine to get the task done. You can explore its GitHub page for more information.

You should be able to install it from the default repository of your Linux distribution. For Ubuntu-based distros, you can type in the command:

sudo apt install wkhtmltopdf

It is pretty straightforward to use no matter whether you want to convert it to a PDF or image file:

htmltopdf mint

To convert a webpage into a PDF, type in:

wkhtmltopdf URL/domain filename.pdf

As an example, here’s how it would look:

wkhtmltopdf linuxmint.com mint.pdf

You can choose to use the complete URL as “https://linuxmint.com” or use the domain name as shown in the example above.

The file generated will be saved in the home directory by default.

You also get a few exciting options when converting a webpage.

For instance, you can apply a grayscale filter to the PDF file, make multiple copies of the page in the same file, and exclude images during conversion.

The grayscale filter may not work on every webpage, but you can try that using the command:

wkhtmltopdf -g google.com googlepage.pdf

To make multiple copies of pages in the same PDF file, the command would be:

wkhtmltopdf --copies 2 linuxmint.com mint.pdf

And, if you want to exclude images from the web pages, just type:

wkhtmltopdf --no-images linuxmint.com mint.pdf

Additionally, if you want to convert a webpage as an image, the command would look like this:

wkhtmltoimage linuxmint.com mint.png

Note that unlike the GUI method using a browser, using these tools via the terminal has its limitation. It does not seem to convert web pages utilizing any <iframe> code snippets successfully.

Sites like ours and even DuckDuckGo didn’t convert to PDF or an image. However, simple HTML sites like Linuxmint.com, Ubuntu.com, Google.com, worked like charm.

Wrapping Up

To get the best results, saving a webpage as a PDF using a browser seems to be the way to go. But, if you want more options and go through the terminal, the wkhtmltopdf utility should come in handy.

How do you prefer to convert web pages to PDF in Linux? Feel free to share your thoughts in the comments.

Similar Posts