How to Convert HTML Files to PDF Format in Linux

There are various options for converting .html to .pdf in a gnu/linux operating system. One very simple option for creating a .pdf file from an .html file is to open the file in your browser, and choose, print. When the print dialog arises, choose "Print to File", and indicate "PDF". Alternatively, you could simply open the file with OpenOffice.org's html editor (ooweb). Then simply go to the "File" menu, and choose "Export". However, command line tools can also be used.

Things You'll Need

  • Gnu/linux operating system
  • Html file (hypertext markup, or web page)
  • Web browser
  • Optional:
  • Openoffice.org office suite
  • wget
  • html2ps
  • ps2pdf
Show More

Instructions

    • 1

      Save the file to your machine using wget, by entering the following command at the terminal, substituting [URL] for the actual url (uniform resource locator, or web address) of the file in question.

      wget [URL]

      Example:
      you@yourmachine:~$ wget http://www.somewebsite.com/somedirectory/somefile.pdf

      Of course, if the file is already on your machine, this step is not necessary.

    • 2

      Use "cd" (change directory), to navigate in terminal to the directory in which your file is kept on your machine. If you used wget to download the file, it should be in the current working directory, but, clearly, if you already had the relevant file on your machine, you may have saved it to somewhere other than your /home directory.

      cd /path/to/directory

      example:
      you@yourmachine:~$ cd /home/you/Documents/
      you@yourmachine:~/Documents$

    • 3

      Convert the file to postscript, using html2ps with the following command. Substitute "yourfile" with the actual name of the file in question.

      html2ps yourfile.html > yourfile.ps

      Example:
      you@yourmachine:~/Documents$ html2ps yourfile.html > yourfile.ps

    • 4

      Affect the final conversion from postscript to pdf with the following command, again, substituting [yourfile] with the name of your file.

      ps2pdf yourfile.ps

      Example:
      you@yourmachine:~/Documents$ ps2pdf yourfile.ps

      You now have a pdf (portable document format) file, converted from your original html file.

Tips & Warnings

  • The programs, wget, cd, html2ps and ps2pdf are likely to be part of your gnu/linux system by default, or easily installable using the default package manager (synaptic, aptitude, portage, emerge).

  • In many cases, you may wish to create a pdf file from a complex .html or .xhtml file that includes .css (cascading style sheet) or other elements, that will not render in the above methods in such a manner as to produce a file that appears as it does on the Internet.

  • For those cases, there is a program called xhtml2pdf. This program is not as likely to be a part of most gnu/linux distributions by default, nor available from said distributions' repositories. As such, you may to have to download and install it by hand. Thankfully, the site for this program is easily enough found and, of course, the program is free, open source software, and, thus, can be downloaded and installed without cost to the user.

  • You will note, if you test all of the mentioned methods on the same file, that the results will vary, both in appearance and terminology. Choose the method that produces the result most suitable to your needs or objectives for use of the resultant pdf file.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured