OS X has PDF printing support built into all applications, and this PDF support can be used from the command line or in an AppleScript script / application to convert virtually any kind of document to the PDF format.

In the example below there are five HTML files (file1.html, file2.html, …, file5.html) that have to be converted to a single PDF (all.pdf). To accomplish this on the command line, open up a new Terminal window, go to the folder the HTML files are stored in, and optionally execute this command to merge them into one file:

cat * > all.html

Next, call Apple’s ‘convert‘ utility (this is not the ‘convert’ utility related to ImageMagick) and specify the input and output filenames:

/System/Library/Printers/Libraries/convert -f all.html 
  -o all.pdf

The ‘convert‘ utility will build the output file and write it to the folder your Terminal shell is currently focused on. The command to convert to / from other filetypes is similar, except that you can’t ‘cat‘ the input files if they’re not text-based.

This functionality can be automated by writing an AppleScript application that makes use of the command line tool. Enter the following code into Script Editor (found in the Utilities folder in your Applications folder):

on open input_documents
  repeat with this_document in input_documents
    set this_document_path to POSIX path of this_document
    --display dialog this_document_path
    do shell script "/System/Library/Printers/Libraries/convert -f " & quoted form of this_document_path & " -o " & quoted form of this_document_path & ".pdf"
  end repeat
end open

Click ‘File->Save‘ to save the script’s source code, then ‘File->Save As…‘ and change the File Format to Application to save an executeable (application) version of the script. If you drop documents on this application, it will automatically launch and create PDF copies of the documents in their source folder.

Share this article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Twitter
  • GatorPeeps
  • Digg
  • Reddit
  • muti.co.za
  • DZone
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Ma.gnolia
  • Slashdot

Related posts:

  1. Change the OS X Network Location from the command line or an AppleScript application
  2. Mac OS X Quick Tip: Launch the screen saver with a bash script using AppleScript
  3. Resize Images Using AppleScript
  4. Mac OS X Quick Tip: Using Spotlight to search from the command line
  5. Creating shortened URLs from the command line