page_count of an existing pdf

592 views
Skip to first unread message

Sebastian Schasse

unread,
Sep 25, 2014, 5:38:50 AM9/25/14
to prawn...@googlegroups.com
Hi there,

I just check out the prawn pdf library and need to count the pages of an existing pdf. I use prawn version 1.2.1 and ruby version 2.1.2.

After some research I found posts, saying that this should work:

Prawn::Document.new(template: "/path/to/some.pdf").page_count

But actually it does not. The template option for Prawn::Document does not exist anymore. But there is another gem called prawn-templates, which extracted this option.
With this gem I get the following Error:

> Prawn::Document.new(template: generated_pdf).page_count                                                                                                          
NoMethodError: undefined method `margins' for nil:NilClass
from /home/user/.rvm/gems/ruby-2.1.2/gems/prawn-1.2.1/lib/prawn/document.rb:686:in `
generate_margin_box'

Any suggestions?

cheers
Sebastian

Gregory Brown

unread,
Sep 25, 2014, 6:52:44 AM9/25/14
to prawn...@googlegroups.com
Because our template system had a design that lead to corrupt documents,
we removed the feature. The prawn-templates gem was an attempt to
extract the relevant code so that others might be able to maintain
it if they wanted, but that hasn't happened.

Please don't use this broken feature, but if you *must* use it, you can
try Prawn 0.15.0 with the prawn-templates gem.

That said if you're just trying to get the page count of an existing
PDF, you can use our testing tool, PDF::Inspector for that.

page_counter = PDF::Inspector::Page.analyze(@pdf.render)
p page_counter.pages.size

More details here: https://github.com/prawnpdf/pdf-inspector

-greg

On 9/25/14 5:38 AM, Sebastian Schasse wrote:
> Hi there,
>
> I just check out the prawn pdf library and need to count the pages of an
> existing pdf. I use prawn version 1.2.1 and ruby version 2.1.2.
>
> After some research I found posts, saying that this should work:
>
> |
> Prawn::Document.new(template:"/path/to/some.pdf").page_count
> |
>
> But actually it does not. The template option for Prawn::Document does
> not exist anymore. But there is another gem called prawn-templates,
> which extracted this option.
> With this gem I get the following Error:
>
> |
>>Prawn::Document.new(template:generated_pdf).page_count
> NoMethodError:undefinedmethod `margins' for nil:NilClass
> from
> /home/user/.rvm/gems/ruby-2.1.2/gems/prawn-1.2.1/lib/prawn/document.rb:686:in
> `generate_margin_box'
> |
>
> Any suggestions?
>
> cheers
> Sebastian
>
> --
> You received this message because you are subscribed to the Google
> Groups "Prawn" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to prawn-ruby+...@googlegroups.com
> <mailto:prawn-ruby+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Gregory Brown

unread,
Sep 25, 2014, 6:53:21 AM9/25/14
to prawn...@googlegroups.com
Sorry, please replace @pdf.render below with something like
File.binread("your.pdf")

Sleepless in the night

unread,
Sep 27, 2014, 6:35:22 AM9/27/14
to prawn...@googlegroups.com
Hi,

You can try the combine_pdf gem.

Your code will look something like this:

pdf = CombinePDF.load("path/to/some.pdf")
# get the length
pdf
.pages.length
# or the author
pdf
.author
# or the title
pdf
.title


or a one-liner just for length:

CombinePDF.load("path/to/some.pdf").pages.length


The gem is still new and I will appreciate any help testing it and any reported issues.

Good luck!
Reply all
Reply to author
Forward
0 new messages