Search code examples
ruby-on-railsherokujrubyitextpdftk

How to set up pdftk or iText to work with Rails 3 on Heroku?


I'm trying to find a way to inject FDF file content into a fillable PDF file (provided by customer and not supposed to be 're-drawn' using Prawn or PDFKit), and I think I have to use either iText(with JRuby) or pdftk.

Both of these libs work with no problem on my Ubuntu local machine, but I am wondering how I would get either them to work on Heroku. Has anyone tried to get iText(JRuby) or PDFTK to work on Heroku ?

Thanks for your help!


Solution

  • You can use pdftk on Heroku – it's pre-installed. You use it by shelling out to it as a command-line program from within your Ruby app.

    For most pdftk commands, you will use Tempfiles. Interpolate the Tempfile#paths in the arguments to pdftk.

    `pdftk #{subcommand}`
    raise Exception unless $?.success?
    

    In some cases, you will be able to pipe data in and out using stdin and stdout rather than Tempfiles.

    input = ...
    output = IO.popen "pdftk #{subcommand}", 'w+b' do |io|
      io.write input
      io.flush
      io.close_write
      io.read
    end