DEV Community

Cover image for Adding a title page to your PDF using CombinePDF
chowderhead
chowderhead

Posted on

Adding a title page to your PDF using CombinePDF

  • 5 min read
  • assumptions:
    • you know ruby
    • you have a rails env
    • you have combinePDF installed

CombinePDF

https://github.com/boazsegev/combine_pdf/blob/master/README.md

Lets go!

start by creating a new instance of CombinePDF

pdf = CombinePDF.new

Enter fullscreen mode Exit fullscreen mode

now that we have an instance , lets add PDFS to it .

  • CombinePDF.load will combine pdf's into our instance using file paths

  • we will need to use << to add it to the pdf instance

  • here well add each file path to an array to make life easier


filepaths = []
filepaths << 'some/path/chickens1.pdf'
filepaths << 'some/path/cows2.pdf'
filepaths << 'some/path/froglegs3.pdf'

Enter fullscreen mode Exit fullscreen mode

Now that we have an array of file paths , lets iterate through the array and use CombinePDF.load to load the PDF's into our CombinePDF instance.

filepaths.each do |path|
    file = CombinePDF.load(path)
    pdf << file if file
end
Enter fullscreen mode Exit fullscreen mode

Now that we have a pdf array lets add some page numbers to our combined PDF document :

pdf.number_pages location: [:bottom_right], number_format: 'Page %s', font_size: 10, opacity: 0.5

Enter fullscreen mode Exit fullscreen mode

Great ! now the final step is to set an Output path for our combined PDF document:


output_filepath = "#{Rails.root}/tmp/combined_#{Time.now.to_i}.pdf"

Enter fullscreen mode Exit fullscreen mode

And the final step is to save

pdf.save output_filepath

Enter fullscreen mode Exit fullscreen mode

We just saved to our output_filepath lets go have a look:

alt text

OOoops

looks like we do have a pdf instance with some PDF's , but I don't want to add a page number to the first file, I want that file to act as a title page for my document.

Lets go back to the drawing board, we need to add distinguish the first file from the others, and start the page counting at the 2nd file in the array.


filepaths.each_with_index do |path,i|
      if i > 0
        file = CombinePDF.load(path, allow_optional_content: true)
        pdf << file if file
      end
    end

Enter fullscreen mode Exit fullscreen mode

if we use each_with_index here , and start at the second index we can skip the first file for now and immediately call the numbering method right after.

After numbering the pages, with the first file left out - lets add it back in, but at the front of the array



# combine the file path we skipped over into the PDF instance 
title_page = CombinePDF.load(filepaths[0], allow_optional_content: true)
pdf >> title_page if title_page

Enter fullscreen mode Exit fullscreen mode

notice that we are using >> this tells CombinePDF that we want to combine the file at the beginning of the page

if we want to do anything later with the title page we can set it to a variable:

title_page = pdf.pages[0]

Enter fullscreen mode Exit fullscreen mode

Lets run it and see what we get !

alt text

Nice!

Heres the final code:

pdf = CombinePDF.new

filepaths = []

filepaths << 'some/path/chickens1.pdf'
filepaths << 'some/path/cows2.pdf'
filepaths << 'some/path/froglegs3.pdf'

filepaths.each_with_index do |path,i|
      if i > 0
        file = CombinePDF.load(path, allow_optional_content: true)
        pdf << file if file
      end
    end

pdf.number_pages location: [:bottom_right], number_format: 'Page %s', font_size: 10, opacity: 0.5

title_page = CombinePDF.load(filepaths[0], allow_optional_content: true)
pdf >> title_page if title_page

output_filepath = "#{Rails.root}/tmp/combined_#{Time.now.to_i}.pdf"

pdf.save output_filepath


Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
senorlocksmith profile image
senorlocksmith

I would love to see a little more detailed information for how to implement. For beginners there is little information on where to put all this code, what is best practice to place in controller, helper or another class. Also having a view component would be great. What does the link look like etc. There are very few sites that offer help for self learning beginners.

Collapse
 
karianewalsh profile image
KarianeWalsh

Until I visit your site it was very complicated for me to complete that program. I must appreciate your skills in the coding. I got more helpful hints from you and it was great. Well I am glad that day by day I am learning new things from your source.

Collapse
 
nodefiend profile image
chowderhead

my first post ! please help me out with any critiques you might have ! or if this helps you at all it would make my day to hear about it!