<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=706355809571598&amp;ev=PageView&amp;noscript=1">
Free Assessment
Estimate Project
Menu
Estimate Project

Merge a bunch of Pdfs into a resulting one with Prawn

by Stéphane A., on 08 March 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 !

1
2
3
4
5
6
7
8
9
10
11
12
13
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 !

New Call-to-action

 
Topics:Under the hood

Comments