Re: Problems with multipage PDF Template

486 views
Skip to first unread message

Christopher Jaeger

unread,
Jun 22, 2012, 11:40:15 AM6/22/12
to prawn...@googlegroups.com
Define a method start_new_page(options)

def start_new_page(options={})
  opts = options.reverse_merge!({
    :template => path_to_template 
  })
 super(opts)
end

On Fri, Jun 22, 2012 at 8:17 AM, 42GB <42g...@googlemail.com> wrote:
Hi Group,

I'm just working on a PDF-Generator. I want to use a fancy PDF as a template. The template has 2 pages. I'm trying to achieve to get the first page of the template on the first page of the new generated pdf and the second page of the template on all following pages of the new generated pdf. First step is no Problem. I'm generating a pdf with this:

Prawn::Document.generate(filename, :page_size => "A4", :skip_page_creation => true) do |pdf|

# Afterwards i set the template for the first site like this:

pdf.start_new_page(:template => path_to_template, template_page => 1)

Now i'm generating content. And this content is getting longer and longer... At some point a new page is automatically generated, but without any template. Because of not running the method #start_new_page on the second page, i have no template. Just a Guess!

The Question is:  But how is it possible to define :template for following pages, which are not especially generated by the method #start_new_page? Did i miss something? I'm happy about every bit of information.

Cheers, Georg 

42GB

unread,
Jun 22, 2012, 12:29:57 PM6/22/12
to prawn...@googlegroups.com
HAristata,

thanks for your response and your tip. For my understanding: This new defined method is automatically called, if the page breaks, right?
So i have to define that method in the context of Prawn::Document, so that the pdf = Prawn::Document.new() can access it. I don't get it. 
I have to try it out, at the moment it looks more like a ruby problem to me. I'll send updates. THX.

Cheers, Georg


Am Freitag, 22. Juni 2012 17:40:15 UTC+2 schrieb Aristata:
Define a method start_new_page(options)

def start_new_page(options={})
  opts = options.reverse_merge!({
    :template => path_to_template 
  })
 super(opts)
end

Christopher Jaeger

unread,
Jun 22, 2012, 12:31:33 PM6/22/12
to prawn...@googlegroups.com
Correct, you are overriding the method which *is* called automatically when a new page begins, passing it your options, then letting super take care of the rest!

42GB

unread,
Jun 24, 2012, 12:30:27 PM6/24/12
to prawn...@googlegroups.com
Hello Aristata,

For me as a ruby noob i was wondering how i could implement this. Now, after some research i found a few solutions for this. But there was no example which overwrote a method in a block. I'm just wondering if my solution is ok or if there is a nicer one out there. 

    Prawn::Document.generate(filename, :page_size => "A4", :skip_page_creation => true) do |pdf|
      
      def pdf.start_new_page(options={})
        opts = options.reverse_merge!({
          :template => "pdf/template/template.pdf",
          :template_page => 2
        })
       super(opts)
      end

      #generate the first page 
      pdf.start_new_page(:template => "pdf/template/template.pdf", :template_page => 1)

      # generate more content
      #..........
      # if page breaks, new page with this parameters is created :template => "pdf/template/template.pdf", :template_page => 2
    end  

This works pretty good. But i never saw a method definition on an object (def pdf.start_new_page) in ruby before, except "self" of course. I see no other solution to overwrite this method in this context.

Cheers, Georg


Am Freitag, 22. Juni 2012 18:31:33 UTC+2 schrieb Aristata:
Correct, you are overriding the method which *is* called automatically when a new page begins, passing it your options, then letting super take care of the rest!

Christopher Jaeger

unread,
Jun 24, 2012, 2:08:37 PM6/24/12
to prawn...@googlegroups.com
Hey Georg,

I am not entirely sure what you're asking here, but this might help you out(?):

This is the method you are overwriting: http://prawn.majesticseacreature.com/docs/0.11.1/Prawn/Document.html#method-i-start_new_page

What you have written there should work, whether or not it is the most elegant solution I do not know! There are always many ways to tackle a problem, in my experience with prawn I have found great success overwriting that method to get the results I want, one thing to note is that you can use this method, http://prawn.majesticseacreature.com/docs/0.11.1/Prawn/Document.html#method-i-page_count, to replace the hardcoded "page 2".

Here is some of my code where I am overwriting that method:

  class CustomizedPDF < Prawn::Document
    include Prawn::Core::Text
    include Prawn::Text::Formatted
    
    def start_new_page(options={})
      super
      if is_person?
        apply_normal_margins
        apply_first_page_margins if !@sidebar_end_page
        person_footer(opts) if draw_person_footer?(opts)
        if !@sidebar_end_page
          apply_sidebar_margins
        elsif page_number > @sidebar_end_page
          apply_normal_margins
        else
          apply_first_page_margins
        end
      else
        apply_normal_margins
        build_header
      end
    end

 end
end

As you can see, I am not actually passing anything into super because I don't want to modify what super does as you do, but I am applying additional logic to set my page margins and write in a 'rasterish' format, this allowed me to put content on the page where ever I felt necessary, sidebars, headers, etc. Note many of the method I have called are my own methods you will not find in prawn.

42GB

unread,
Jun 26, 2012, 7:42:58 AM6/26/12
to prawn...@googlegroups.com

Hey Christopher,

Thank you for your comment and your inside view. Helped me a lot to understand prawn and the dynamic aspect of ruby.

Cheers, Georg

Reply all
Reply to author
Forward
0 new messages