8

I'm trying to convert a bunch of HTML files into PDF files. I'm looking for an automated solution so that i don't have to open each HTML file individually and convert it to PDF. In Mac OSX, convert utility can be used to convert HTML files to PDF. I'm facing two problems.

  1. convert utility is not applying CSS style to generated PDF content.
  2. My AppleScript for batch convert is not working.

Here's my AppleScript:

on open input_documents
    repeat with this_document in input_documents

        if this_document is not document file then
            set this_document_path to POSIX path of this_document
            do shell script "/System/Library/Printers/Libraries/convert -f " & quoted form of this_document_path & " -o " & quoted form of this_document_path & ".pdf"
        end if
    end repeat
end open

I use this script to create a .app and then drag the documents folder onto the generated .app file. Note that all images associated with images are placed in a subdirectory/subfolder. My document structure looks like this:

/HTML
/HTML/Images/
/HTML/a.html
/HTML/b.html

Any suggestions?

4
  • @Mustafa You should avoid cross-posting -- ask a moderator to migrate it for you.
    – Austin
    May 9, 2011 at 5:51
  • I believe stackoverflow has more visibility than stackexchange. Duplicate won't hurt, it might just work for me.
    – Mustafa
    May 9, 2011 at 6:16
  • 2
    Have you looked into the textutil command? It also supports HTML.
    – user588
    May 9, 2011 at 6:39
  • 2
    @mankoff, I did. It doesn't support the conversion of html files to pdf. It supports: txt, html, rtf, rtfd, doc, docx, wordml, odt, or webarchive
    – Mustafa
    Jun 7, 2011 at 10:58

3 Answers 3

19

Have a look at wkhtmltopdf— a command-line utility that uses the WebKit rendering engine to produce PDFs from HTML. I've found that it produces a nicer result. You shouldn't have any trouble integrating it with your current script.

3
  • Just ran ./wkhtmltopdf *.html *.pdf and it worked great, with an exception. It produced "*.pdf" file instead of separate files for each html. Do you know if this utility has the support to generate separate files? Thanks much.
    – Mustafa
    May 9, 2011 at 6:14
  • 4
    Hi Mustafa, try something like (in a directory of HTML files) for f in *.html; do wkhtmltopdf $f "$f.pdf"; done to loop through the files in a directory.
    – Matt
    May 9, 2011 at 6:47
  • 3
    With a little modification e.g. for f in *.html; do ./wkhtmltopdf "$f" "$f.pdf", it works beautifully. Thanks much.
    – Mustafa
    May 9, 2011 at 7:03
1

Have you considered writing an AppleScript script to open each HTML file in a scriptable web browser (like Safari) and then using the browser's standard print command to print to a PDF file?

2
  • Hm, i'm not that good with AppleScript :) In fact, i've recently only done Hello World like stuff in AppleScript.
    – Mustafa
    May 9, 2011 at 5:49
  • Chrome's print screen command on certain sites (this site for example) does not produce an exact replica of the html on the site. So the more accurate wkhtmltopdf solution might be necessary, depending on needs. Feb 10, 2013 at 6:23
0

I got tired of rewriting my AppleScript with every new system version, and have switched to running Google Chrome headlessly instead; it's much faster, because you don't have to wait for the scripted UI to play. If you have Google Chrome installed in the standard location, this should work (assuming the name of the file you want to print is stored in the variable called $file):

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome  --print-to-pdf --headless "$file" > `basename "$file" .html`.pdf

Note that if you are (like me) running Chrome and Terminal in different architectures, you will need to prefix the command with "arch --arm64" or "arch --x86_64".

1
  • 1
    In my version of chrome (114.0.5735.133) I had to use --print-to-pdf='filename.pdf' to specify the output filename. When redirecting the output, I would just get an empty file (and the correct file named output.pdf).
    – Fizk
    Jun 23, 2023 at 6:38

You must log in to answer this question.

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