Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com May 9 '11 at 10:55

This question came from our site for professional and enthusiast programmers.

    
Thanks for the tip. I'll post there. –  Mustafa May 9 '11 at 5:50
    
@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
    
@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

2 Answers 2

up vote 9 down vote accepted

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.

share|improve this answer
    
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
2  
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
    
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?

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.