book a call

estimate project

The belighted Blog

Merge a bunch of Pdfs into a resulting one with Prawn

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

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 = => pdf_file).page_count 
 (1..pdf_temp_nb_pages).each do |i| 
 pdf.start_new_page(:template => pdf_file, :template_page => i) 

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

Topics:      Under the hood

What is SaaS?

Everything You Need to Know About Moving to a SaaS Model.

Get the guide now >

Subscribe to Updates