8

How can I convert a html book (consisting of a number of html files linked to each other) to a pdf file on Ubuntu?

Hope the resulting pdf file can have bookmarks according to the structures of the html book.

The html book can be downloaded from here. Extract it, and go to help, and the directory manual is the html book.

6
  • 1
    Have you taken a look at pandoc? It manages to convert between tons and tons of things (including PDF and HTML).
    – HalosGhost
    Commented Jun 24, 2015 at 3:45
  • thanks. how shall I use pandoc to convert?
    – Tim
    Commented Jun 24, 2015 at 3:46
  • Their documentation should take care of that :)
    – HalosGhost
    Commented Jun 24, 2015 at 4:07
  • thanks. in the extracted directory from the zip file, the html book is in the directory help/manual. Its first page is index.html, which contains links to a lot of other htmls in directory help/manual/man. Do I have to specify all the html files as inputs to pandoc, or just index.html and no other html files as input to pandoc? If the former, how do you specify the many html files in the correct order?
    – Tim
    Commented Jun 24, 2015 at 4:15
  • That is a good question of which I know not the answer. Though I know of pandoc, I have never personally had a need to use it.
    – HalosGhost
    Commented Jun 24, 2015 at 4:20

7 Answers 7

6

You can use chrome headless

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum localinstall google-chrome-stable_current_x86_64.rpm
./chrome --headless --no-sandbox --disable-gpu --print-to-pdf index.html
3
  • There are multiple html files, and index.html might just be the one with links to the others. Will your chrome command only print the index.html or also the html files linked from it?
    – Tim
    Commented Apr 22, 2020 at 11:41
  • I've actually only tried it with 1 html file
    – gary69
    Commented Apr 23, 2020 at 2:53
  • Renders better than wkhtmltopdf. Lots of errors in the terminal though, and it can't do fonts with gradients. Extra features here stackoverflow.com/questions/46077392/…
    – Jay Brunet
    Commented Apr 30, 2023 at 8:17
6

Just use wkhtmltopdf

https://github.com/wkhtmltopdf

On my Fedora:

  • install via
    dnf install wkhtmltopdf 
    
  • use as
    wkhtmltopdf in.html out.pdf
    
2
  • 2
    Welcome to the site, and thank you for your contribution. Please note that using wkhtmltopdf was already suggested in this answer by @Wyatt8740; you may want to edit yours to make the difference clearer.
    – AdminBee
    Commented Jul 23, 2020 at 11:02
  • 1
    pdf output looks ¦ ̧à§​টà§​রà¦3⁄4টোঠ̧à§​ফিয়à¦3⁄4রের
    – alhelal
    Commented May 4, 2022 at 17:23
4

First off, not sure I can get the bookmarks and such made automatically, as nice as that would be.

You can manually add them, though, afterwards.

Try opening the HTML file in firefox, and going to file -> print (ctrl+p). Then, click "Print to File" and click 'PDF'. Then type your file name and select the folder to save it in.

enter image description here

If you want to automate it, maybe you want wkhtmltopdf? To install it (in ubuntu or debian or linuxmint or similar), type:
sudo apt-get install wkhtmltopdf

2
  • wkhtmltopdf was perfect!
    – niCk cAMel
    Commented Jan 3, 2020 at 13:24
  • Great! Glad my answer helped someone, all these years later. I'd totally forgotten writing this answer…
    – Wyatt Ward
    Commented Jan 4, 2020 at 11:06
4

You can use package htmldoc, which is much easier to get working than pandoc. It takes a list of html files and resolves the hyperlinks between them to produce a single pdf with working links, and includes the images.

htmldoc $(find help/manual -name '*html' | sort) --outfile  /tmp/out.pdf

Of course, it would help to know in what order to collate the html files. I just sorted them on name.

Unfortunately, on my fedora 20 I got core dumps when I tried to put more than half the audacity files through at the same time. I got to 440 pages of output though. Perhaps just not enough memory. YMMV.

Remember for safety to prefer the real audacity web site or your OS package manager rather than sourceforge.

3

Some other alternatives:

weasyprint - more up to date and better support for print css commands than something like wkhtmltopdf

pagedjs / pagedjs-cli - similar; uses headless chromium in the backend, but abstracts away some of the difficulties

I recommend reading this tutorial at print-css.rocks, which summarizes a bunch of methods.

1

You probably alredy have Libreoffice installed so you already have the tool at hand. Here is how:

$ libreoffice --headless --norestore --convert-to pdf:writer_pdf_Export MY_HTML_FILE.html

If you use windows just replace libreoffice for the equivalent .exe file ( probably libreoffice.exe ).

If you dont have libreoffice installed just go to libreoffice.org for instructions on how to install it.

On unbutu $ sudo apt install libreoffice

0

I have print to pdf a bunch of html files using "chromium headless" (the chrome base browser).

chromium --headless --disable-gpu --print-to-pdf=./${filename}.pdf --no-margins ./${filename}.html

A bash script to do the job:

#!/usr/bin/env bash

for files in $(ls . )
do
    filename=`echo $files | cut -d. -f1`
    chromium --headless --disable-gpu --print-to-pdf=./${filename}.pdf --no-margins ./${filename}.html
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .