Skip to content

Instantly share code, notes, and snippets.

@eclosson
Created January 11, 2013 18:29
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save eclosson/4512859 to your computer and use it in GitHub Desktop.
Save eclosson/4512859 to your computer and use it in GitHub Desktop.
Merging PDFs with Prawn
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
@canjianx
Copy link

canjianx commented Mar 6, 2014

D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/pdf-core-0.1.6/lib/pdf/core
/object_store.rb:22:in initialize': undefined methodload_file' for #<PDF::Cor
e::ObjectStore:0x2a33a68 @objects={}, @Identifiers=[]> (NoMethodError)
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/pdf-core-0.1.6
/lib/pdf/core/document_state.rb:8:in new' from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/pdf-core-0.1.6 /lib/pdf/core/document_state.rb:8:ininitialize'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/prawn-0.15.0/l
ib/prawn/document.rb:206:in new' from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/prawn-0.15.0/l ib/prawn/document.rb:206:ininitialize'
from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/prawn-0.15.0/l
ib/prawn/document.rb:146:in new' from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/prawn-0.15.0/l ib/prawn/document.rb:146:ingenerate'
from testprawn.rb:14:in merge' from testprawn.rb:25:in

'

@nikgoy
Copy link

nikgoy commented Mar 15, 2014

@canjianx

getting the same error.

Were you able to find any fix for it.

--EDIT--

I guess the latest version of Prawn PDF does not support Templates anymore and so this would not work.

@asanger
Copy link

asanger commented Apr 2, 2014

I'm not getting an error, I'm just getting blank PDFs.

@uzzer
Copy link

uzzer commented Apr 16, 2014

I am also getting blank PDFs.

@bhh
Copy link

bhh commented May 16, 2014

prawn removed support for :template as of 0.14.0 :( causing the blank pages

@belgoros
Copy link

belgoros commented Aug 5, 2014

Officially, Prawn dropped template support, so it will not work.
See more about that: prawnpdf/prawn#376

PS. Sorry, didn't see @bhh post :)

@wakproductions
Copy link

👎🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment