public
Created

Merging PDFs with Prawn

  • Download Gist
pdf_merger.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
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

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 # 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:in
initialize'
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:in
initialize'
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:in
generate'
from testprawn.rb:14:in merge'
from testprawn.rb:25:in
'

@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.

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.