Reputation: 11238
I am looking to convert an html file to a pdf without any 3rd party utilities. I am not thrilled with the output from cupsfilter and I don't want to script a browser. This AppleScript / Ruby script used PDFKit to resave a pdf (to get around an InDesign problem). Is there any way of modifying it to accept an html file as input? Any other solutions are welcome too!
on pdf_resave(infile, outfile)
set rb to "require 'osx/cocoa'
OSX.require_framework 'PDFKit'
include OSX
raise ArgumentError, %Q[Usage: $0 <input pdf> <output pdf>] unless ARGV.length == 2
infile, outfile = ARGV
url = NSURL.fileURLWithPath(infile)
pdf = PDFDocument.alloc.initWithURL(url)
pdf.writeToFile(outfile)"
do shell script "/usr/bin/ruby -e " & rb's quoted form & " " & infile's quoted form & " " & outfile's quoted form
end pdf_resave
Upvotes: 1
Views: 2621
Reputation: 30319
I'd suggest using the webkit HTML to pdf library, https://github.com/antialize/wkhtmltopdf.
You can just call the binary from within Ruby.
Install it with:
brew install wkhtmltopdf
You can package it up into a bundle with the Ruby script if deployment/dependency concerns are an issue. You'd simply add a checkout and compile stage to the install script (thus avoiding the need to install brew
.) - you can package a pre-made binary this way too, as long as you test it on the target platforms (I'd suggest 10.6 up and you're safe).
For more info on making a .pkg
install bundle see this succinct guide.
That's all overkill though, because, as far as I can see at least, wkhtmltopdf
does the job for you from start to finish, other than feeding it a batch of html files, in which case wrap it in a shell script, and stop mucking about ;)
Upvotes: 2