# Cut first x pages from a PDF file

**URL:** <https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543>\
**Category:** Rails\
**Created:** [April 25, 2010, 12:59pm UTC](https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543 "2010-04-25T12:59:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 25, 2010, 12:59pm UTC](https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543/1 "2010-04-25T12:59:53Z")

</div>

Hi all,

I’m finishing up on a project, which has a model with an attached PDF  
file. I’m using Paperclip to process the attachment.

What I’d like to do is to enable users to cut (remove) the first x  
pages from the PDF-file, but I have no idea how to do that. Is there a  
gem that can do this? And how would this integrate with Paperclip?

Thank you very much.

Kind regards,  
Jaap H.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 25, 2010, 1:52pm UTC](https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543/2 "2010-04-25T13:52:34Z")

</div>

Take a look at Paperclip Processors  
Paperclip could use custom processors to do non-standart stuff with  
attachments. You specify your processor name like:

has\_attached\_file :file,  
:styles =\> {:original =\> {:processors =\> [:pdf\_processor]}}

and you need to have a class in  
lib/paperclip\_processors/pdf\_processor.rb like

module Paperclip  
class PdfProcessor \< Paperclip::Processor  
…  
end  
end

there’s some examples in documentation. The thing is you get an uploaded  
@file in your processor, and in the end you need to return your changed  
@new\_file back. You could do anything with it inside.

Like in your case, you can create some tmp\_dir, process the @file.path  
with  
something like `pdftk` to your tmp\_dir/new\_file\_name.pdf, and return  
@new\_file = File.open(tmp\_dir/new\_file\_name.pdf) to Paperclip to store  
in  
path you specified in the Model. Then you just delete tmp\_dir with  
after\_create filter or with cron job.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 25, 2010, 3:47pm UTC](https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543/3 "2010-04-25T15:47:51Z")

</div>

Hi Vladimir,

Thanks. The bit about paperclip processors is very helpful!

I’ve looked at pdftk though and couldn’t find how to cut pages from  
PDF files. Am I overlooking something? The idea is that we will  
receive many different kinds of PDF-files including (for example)  
author information that the author doesn’t want to share. If this  
information is on the first two pages, we’d like the author to be able  
to say “cut the first two pages out and save it”.

Thanks again.

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 25, 2010, 3:50pm UTC](https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543/4 "2010-04-25T15:50:46Z")

</div>

I don’t think that you can explicitly do that with pdftk, but you can do  
‘burst’ to break the pdf out into lots of single page documents, and  
then  
‘cat’ to combine the pages that you want in the final document.

Simon

On Sun, 25 Apr 2010 21:46:23 +0800, jhaagmans [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jhaagmans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jhaagmans](https://www.ruby-forum.com/u/jhaagmans)\
**Post date:** [April 25, 2010, 5:21pm UTC](https://www.ruby-forum.com/t/cut-first-x-pages-from-a-pdf-file/187543/5 "2010-04-25T17:21:31Z")

</div>

Well, you can use ranges with pdftk. Like

$ pdftk in.pdf cat 3-end output out.pdf

Catenates pages from third till the end. Here are good examples of pdftk  
usage

> **[PDFtk - The PDF Toolkit](https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/)**
>
> PDFtk is a simple tool for doing everyday things with PDF documents. It comes in three flavors: PDFtk Free, PDFtk Pro, and our original command-line tool PDFtk Server.
