Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you combine PDFs in ruby?

This was asked in 2008. Hopefully there's a better answer now.

How can you combine PDFs in ruby?

I'm using the pdf-stamper gem to fill out a form in a PDF. I'd like to take n PDFs, fill out a form in each of them, and save the result as an n-page document.

Can you do this with a native library like prawn? Can you do this with rjb and iText? pdf-stamper is a wrapper on iText.

I'd like to avoid using two libraries (i.e. pdftk and iText), if possible.

like image 226
Paul Schreiber Avatar asked Aug 17 '10 05:08

Paul Schreiber


People also ask

How do I combine multiple PDFs into one?

Open Acrobat to combine files: Open the Tools tab and select "Combine files." Add files: Click "Add Files" and select the files you want to include in your PDF. You can merge PDFs or a mix of PDF documents and other files.

How do I combine PDF files in Linux?

Combine PDFs with the gs commandUse the -sDEVICE attribute to specify the output device or function. Use the -sOUTPUTFILE to specify the merged PDF file. Use the -dBATCH to specify the PDF files to combine in the order you want them to appear. The command above will output merged_file.

Can blue beam combine PDFs?

Multiple PDFs can be combined into a single PDF from within Revu or from the context (right-click) menu in Windows Explorer . Note: Certified PDFs cannot be combined with other PDFs. Additionally, combining PDFs that have been digitally signed, but not certified, will remove the signatures from the combined PDF.


2 Answers

As of 2013 you can use Prawn to merge pdfs. Gist: https://gist.github.com/4512859

class PdfMerger

  def merge(pdf_paths, destination)

    first_pdf_path = pdf_paths.delete_at(0)

    Prawn::Document.generate(destination, :template => first_pdf_path) do |pdf|

      pdf_paths.each do |pdf_path|
        pdf.go_to_page(pdf.page_count)

        template_page_count = count_pdf_pages(pdf_path)
        (1..template_page_count).each do |template_page_number|
          pdf.start_new_page(:template => pdf_path, :template_page => template_page_number)
        end
      end

    end

  end

  private

  def count_pdf_pages(pdf_file_path)
    pdf = Prawn::Document.new(:template => pdf_file_path)
    pdf.page_count
  end

end
like image 95
Evan Closson Avatar answered Sep 28 '22 04:09

Evan Closson


After a long search for a pure Ruby solution, I ended up writing code from scratch to parse and combine/merge PDF files.

(I feel it is such a mess with the current tools - I wanted something native but they all seem to have different issues and dependencies... even Prawn dropped the template support they use to have)

I posted the gem online and you can find it at GitHub as well.

you can install it with:

gem install combine_pdf

It's very easy to use (with or without saving the PDF data to a file).

For example, here is a "one-liner":

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

If you find any issues, please let me know and I will work on a fix.

like image 26
Myst Avatar answered Sep 28 '22 05:09

Myst