Adding a title page to your PDF using CombinePDF
Tags: ruby, rails, pdf, combinepdf
Published: 2018-04-18 09:10:31

---
title: Adding a title page to your PDF using CombinePDF
published: true
description: A short article on how to add a title page to your generated PDF document 
tags: Ruby, rails , PDF , CombinePDF
cover_image: https://thepracticaldev.s3.amazonaws.com/i/uwc155kjc12m4lsxys9s.jpg 
---


> - 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`

```ruby
pdf = CombinePDF.new

```

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 


```ruby

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

```


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.

```ruby
filepaths.each do |path|
    file = CombinePDF.load(path)
    pdf << file if file
end
```

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


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

```

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

```ruby 

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

```

And the final step is to save 

```ruby
pdf.save output_filepath

```

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


    

![alt text](https://thepracticaldev.s3.amazonaws.com/i/dv8mumrhouzo4cmc48hi.png "page number on the title page")

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

```ruby

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

```

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 

```ruby


# 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

```

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: 

```ruby
title_page = pdf.pages[0]

```

Lets run it and see what we get ! 


![alt text](https://thepracticaldev.s3.amazonaws.com/i/70lld737zvsbqrg10o7c.png "No number on the title page")

## Nice! 

Heres the final code: 


```ruby
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


```