How do you combine PDFs in ruby?

前端 未结 8 2132
你的背包
你的背包 2020-12-25 15:17

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.

8条回答
  •  不知归路
    2020-12-25 15:41

    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
    

提交回复
热议问题