How to Combine a Series of PDFs into One Using Ruby

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

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.

How do you combine PDFs in ruby?

I wrote a ruby gem to do this — PDF::Merger. It uses iText. Here's how you use it:

pdf = PDF::Merger.new
pdf.add_file "foo.pdf"
pdf.add_file "bar.pdf"
pdf.save_as "combined.pdf"

combine_pdf not combining the pdfs

It looks like the README for that library calls .to_pdf when sending the data. Hopefully calling #to_pdf on the pdf object like in the example will fix your issue.

send_data pdf.to_pdf, filename: “my_combined_pdf”, type: "application/pdf"

https://github.com/boazsegev/combine_pdf#rendering-pdf-data

How to merge two pdf's, one created with wicked_pdf and second through paperclip upload

You need to use combined_pdf gem

gem install combine_pdf

pdf = CombinePDF.new
pdf << CombinePDF.load("file1.pdf") # one way to combine, very fast.
pdf << CombinePDF.load("file2.pdf")
pdf.save "combined.pdf"

Otherwise, if you want to do the above code in one line

(CombinePDF.load("file1.pdf") << CombinePDF.load("file2.pdf") << CombinePDF.load("file3.pdf")).save("combined.pdf")

How do I combine a number of images into a single PDF using ruby?

You can use RMagick's ImageList#write:

If the image format indicated by the filename supports multiple images per file (animated images), write writes all the images in the imagelist to a single file.

Example:

require 'rmagick'

image_list = Magick::ImageList.new("image1.png", "image2.png", "image3.png")
image_list.write("images.pdf")


Related Topics



Leave a reply



Submit