delete page?

758 views
Skip to first unread message

joelkeepup

unread,
Mar 25, 2010, 9:35:09 AM3/25/10
to Prawn
Hi, is it possible to delete a page of a pdf with Prawn?

thanks
Joel

Gregory Brown

unread,
Mar 25, 2010, 9:40:22 AM3/25/10
to prawn...@googlegroups.com
On 3/25/10 9:35 AM, joelkeepup wrote:
> Hi, is it possible to delete a page of a pdf with Prawn?

I'm trying to think of where that'd be useful. Can you give me an
example of what you're trying to do?

-greg

joelkeepup

unread,
Mar 25, 2010, 9:49:22 AM3/25/10
to Prawn
sure, so Im using pdf templates branch, which worsk great in most
cases, but of the documents are a bit dynamic, for example one
document requires 3 contacts, but optionally may have 10 additional
contacts.

I need a way to not generate a doc with tons of template pages not
filled in.

So one idea I had was to add a bunch of template contact pages at the
end of the pdf template, and just delete the ones I dont need and fill
out the rest.

Another idea we had was to use 2 pdf templates, 1 for the main doc and
1 for the additional contact pages, then merge them (not sure if prawn
or anything else ruby can merge 2 docs)

If you have other ideas, I would love to hear them...

thanks
Joel

On Mar 25, 9:40 am, Gregory Brown <gregory_br...@letterboxes.org>
wrote:

Gregory Brown

unread,
Mar 25, 2010, 9:57:05 AM3/25/10
to prawn...@googlegroups.com
On 3/25/10 9:49 AM, joelkeepup wrote:

> Another idea we had was to use 2 pdf templates, 1 for the main doc and
> 1 for the additional contact pages, then merge them (not sure if prawn
> or anything else ruby can merge 2 docs)

Since we don't yet support templates, it'll be some time before we can
know how to best work with them. That said, I can see where delete_page
could be useful there. With our new page architecture it should be
fairly straightforward, I just need to think through whether any
complications will come up if I do something as simple as
pages.delete_at(index)

> If you have other ideas, I would love to hear them...

pdftk handles merges, I think.

James Healy

unread,
Mar 25, 2010, 9:57:26 AM3/25/10
to prawn...@googlegroups.com
joelkeepup wrote:
> sure, so Im using pdf templates branch, which worsk great in most
> cases, but of the documents are a bit dynamic, for example one
> document requires 3 contacts, but optionally may have 10 additional
> contacts.

In the context of the templates branch, I could see how this would be a
useful feature. It isn't currently possible, but I think it would be
fairly trivial to implement.

I know Jonathan is keen to introduce support for importing templates on
a page by page basis too, so that would make your second idea viable.

As always with the templates branch, the answer is "hopefully soon" :)

-- James Healy <ji...@deefa.com> Fri, 26 Mar 2010 00:56:55 +1100

Pedro Coelho

unread,
Mar 9, 2012, 2:01:51 PM3/9/12
to prawn...@googlegroups.com
Hey,

sorry for bringing up such an old topic.

Any news on this matter? I'm currently using templates and this 
would be extremely handy.

Cheers,
Pedro Coelho

Marco van Weverwijk

unread,
Jul 12, 2013, 5:22:51 PM7/12/13
to prawn...@googlegroups.com
I also need this feature. I would like to add a table of contents to my document. I don't know how many items we will have, so it could be on multiple pages (page 2, 3 and 4), but it also could be one page.

If i can dynamically can add a page, or delete a page, it would be great.

thanks,
Marco

Marco van Weverwijk

unread,
Jul 14, 2013, 8:17:30 AM7/14/13
to prawn...@googlegroups.com
I created a workaround. It is not perfect, but it works. Create a tempfile and use it as a template. You can skip a page, or add a page in the loop if you like:
 
  def add_or_remove_page()
    temp_report_file_name = "#{Rails.root}/tmp/temp_reports/report-#{@report.id}-#{Time.now.to_f}.pdf"
    report_pdf.render_file(temp_report_file_name)

    report_page_count = count_pdf_pages(temp_report_file_name)
    (1..report_page_count).each do |template_page_number|
      # Add or remove a page here... if template_page_numer == x ....
      @p_pdf.start_new_page(:template => temp_report_file_name, :template_page => template_page_number)
    end
  end   

  def count_pdf_pages(pdf_file_path)
    pdf = Prawn::Document.new(:template => pdf_file_path)
    pdf.page_count
  end


Op vrijdag 12 juli 2013 23:22:51 UTC+2 schreef Marco van Weverwijk het volgende:

Oriol López

unread,
Sep 12, 2014, 7:08:56 AM9/12/14
to prawn...@googlegroups.com
Update: prawn template is not supported in newer versions of prawn, to make templates work, you should install a previous prawn gem version.

Go to Gemfile and replace your actual gem 'prawn', 1.X.X   for gem 'prawn','1.0.0.rc1' (last one that supports templates)
run: bundle update prawn and start the server.

Gregory Brown

unread,
Sep 12, 2014, 7:17:25 AM9/12/14
to prawn...@googlegroups.com
This is a little confusing, but the most recent version of Prawn that
supports templates is not a 1.0.0 RC, but 0.15.0. (Although do
need to install the prawn-templates gem and require "prawn/templates")

The RCs were a cosmic accident that happened years ago. At this point,
they should not be used at all, because...

0.12.0 is the ancient stable version of Prawn
0.15.0 is basically 1.0.0.rc2 plus many bug fixes.
1.x is the new stable line.

So... as far as compatibility goes, there's no benefit in using the
1.0 RCs. They only contain more bugs than the 0.13-0.15 line, and
no additional features.

-greg


On 9/12/14 7:08 AM, Oriol López wrote:
> Update: *prawn template is not supported* in newer versions of prawn, to
> make templates work, you should install a previous prawn gem version.
>
> Go to Gemfile and replace your actual gem 'prawn', 1.X.X for gem
> 'prawn','1.0.0.rc1' (last one that supports templates)
> run: bundle update prawn and start the server.
>
> El domingo, 14 de julio de 2013 14:17:30 UTC+2, Marco van Weverwijk
> escribió:
>
> I created a workaround. It is not perfect, but it works. Create a
> tempfile and use it as a template. You can skip a page, or add a
> page in the loop if you like:
>
> def add_or_remove_page()
> temp_report_file_name =
> "#{Rails.root}/tmp/temp_reports/report-#{@report.id
> <http://report.id>}-#{Time.now.to_f}.pdf"
> report_pdf.render_file(temp_report_file_name)
>
> report_page_count = count_pdf_pages(temp_report_file_name)
> (1..report_page_count).each do |template_page_number|
> # Add or remove a page here... if template_page_numer == x ....
> @p_pdf.start_new_page(:template => temp_report_file_name,
> :template_page => template_page_number)
> end
> end
>
> def count_pdf_pages(pdf_file_path)
> pdf = Prawn::Document.new(:template => pdf_file_path)
> pdf.page_count
> end
>
>
> Op vrijdag 12 juli 2013 23:22:51 UTC+2 schreef Marco van Weverwijk
> het volgende:
>
> I also need this feature. I would like to add a table of
> contents to my document. I don't know how many items we will
> have, so it could be on multiple pages (page 2, 3 and 4), but it
> also could be one page.
>
> If i can dynamically can add a page, or delete a page, it would
> be great.
>
> thanks,
> Marco
>
> --
> 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.

Sleepless in the night

unread,
Sep 27, 2014, 6:28:52 AM9/27/14
to prawn...@googlegroups.com
You can use the combine_pdf gem for this:

# move the PDF from Prawn to CombinePDF
pdf
= CombinePDF.parse prawn_pdf.render
# delete any unwanted pages - we will delete the last three pages:
3.times { pdf.remove -1 }

or, you could use CombinePDF to add only the pages you wanted:

# move the PDF from Prawn to CombinePDF
pdf
= CombinePDF.parse prawn_pdf.render
#open the template you are copying from
template_pdf
= CombinePDF.load "template_file.pdf"
# copy the pages you need from the template
pdf
<< template_pdf.pages[0] << template_pdf.pages[1] << template_pdf.pages[5]



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