Blog

Merge a bunch of Pdfs into a resulting one with Prawn

Icône flèche bleue vers la gauche
Retour au blog
Merge a bunch of Pdfs into a resulting one with Prawn

Merge a bunch of Pdfs into a resulting one with Prawn

March 7, 2013

You have a bunch of Pdf documents and you want to concatenate them all into a resulting one, using the Prawn gem and this small piece of code, it’s now easily doable !

require 'prawn'

pdf_file_paths = ["1.pdf", "2.pdf"]
Prawn::Document.generate("result.pdf", {:page_size => 'A4', :skip_page_creation => true})
do |pdf|
pdf_file_paths.each do |pdf_file|
if File.exists?(pdf_file)
pdf_temp_nb_pages = Prawn::Document.new(:template => pdf_file).page_count
(1..pdf_temp_nb_pages).each do |i|
pdf.start_new_page(:template => pdf_file, :template_page => i)
end
end
end
end

If you want this feature fully integrated in the next Prawn version, just support my issue on Github !

Prêt à créer votre produit logiciel ? Contactez-nous !