Is it possible to combine a series of PDFs into one using Ruby?

If you have ghostscript on your platform, shell out and execute this command:

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=finished.pdf <your source pdf files>


You can do this by converting to PostScript and back. PostScript files can be concatenated trivially. For example, here's a Bash script that uses the Ghostscript tools ps2pdf and pdf2ps:

#!/bin/bash
for file in 01_foo.pdf 02_bar.pdf 03_baz.pdf; do
    pdf2ps $file - >> temp.ps
done

ps2pdf temp.ps output.pdf
rm temp.ps

I'm not familiar with Ruby, but there's almost certainly some function (might be called system() (just a guess)) that will invoke a given command line.


A Ruby-Talk post suggests using the pdftk toolkit to merge the PDFs.

It should be relatively straightforward to call pdftk as an external process and have it handle the merging. PDF::Writer may be overkill because all you're looking to accomplish is a simple append.

Tags:

Pdf

Ruby