Forum How do I...?

Accessing PrinceXML from Ruby?

subimage
Has anyone cooked up a Ruby library to use PrinceXML from?

I'm investigating using it with a Rails application, and would love to not have to adapt the PHP or other code if someone's done it already.
mikeday
Not that I know of personally; it shouldn't be very difficult though, just a matter of calling Prince from Ruby with the appropriate command-line arguments. It's probably easier to figure out how to do that without referring to the PHP implementation, I suspect. :)
subimage
I was mostly interested in grabbing a stream from PrinceXML, and didn't think that was possible through the command line.

Guess further investigations are necessary.
mikeday
If you pass - as the input filename Prince will attempt to read the XML/HTML from the standard input stream, and if you pass - as the output filename Prince will attempt to write the generated PDF to the standard output stream.

One issue is whether you will also read error messages from stderr. This can be tricky, as it may require a separate thread, but not reading the error messages can lead to deadlocks. To avoid this, you can just pass --silent on the command line to stop Prince from writing any error messages, and use --log=FILE to save error messages to a separate log file instead.
subimage
That sounds great...I should be on my way to getting this working then. One thing I'm seeing in the PHP class, that I don't see in the command line reference is the '--server' flag....What's it do?
mikeday
It changes the format of error messages to make them easier to parse (which you won't see anyway if you also use the --silent option), and also adds a finished message to make it easy to see if the document converted successfully or not (which you will see, even with the --silent option).
subimage
I've put together a blog post on my adventures with Prince using Ruby on Rails here:

http://sublog.subimage.com/articles/2007/05/29/html-css-to-pdf-using-ruby-on-rails

You can find my Prince Ruby libraries there, available for download.