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:


Any suggestions?

  • @Mustafa You should avoid cross-posting -- ask a moderator to migrate it for you. – Austin May 9 '11 at 5:51
  • I believe stackoverflow has more visibility than stackexchange. Duplicate won't hurt, it might just work for me. – Mustafa May 9 '11 at 6:16
  • 2
    Have you looked into the textutil command? It also supports HTML. – user588 May 9 '11 at 6:39
  • 1
    @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 '11 at 10:58

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.

  • 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 '11 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 '11 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 '11 at 7:03

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?

  • Hm, i'm not that good with AppleScript :) In fact, i've recently only done Hello World like stuff in AppleScript. – Mustafa May 9 '11 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. – Clayton Stanley Feb 10 '13 at 6:23

You must log in to answer this question.

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