Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for help: Setting footer height or margin on page by page basis #572

Open
scottmascio2115 opened this issue Sep 6, 2016 · 7 comments

Comments

@scottmascio2115
Copy link

I have been using the Wicked Pdf gem and have been loving it, thanks for creating it.

However, I have run into a situation where a need to attach and have visible a large footer to the first page, but not the rest of the pages. To accomplish this I added a margin to the bottom of the body of the pdf as suggested in this post which gives the footer enough room to be visible.

This causes adverse side effects because it adds bottom margin to all of the pages not just this first page. I was hoping to only add margin to the first page and leave the rest of the pages unchanged.

I've tried to accomplish this with the use of javascript and css but have had no luck so far.

Thanks in advance

@scottmascio2115 scottmascio2115 changed the title Setting footer height or margin on page by page basis Request for help: Setting footer height or margin on page by page basis Sep 6, 2016
@unixmonkey
Copy link
Collaborator

The margins are document-global (as if you did page-setup before printing), and this is not supported by wkhtmltopdf (or wicked_pdf).

The workaround to this would be to create the cover as a separate PDF document from the rest, and stitch them together with PDFtk or something like that.

Here are some other related issues that might help you out:

#339
#137
#325
#285
#443

Please let me know how it goes, and how the solution looked for future developers that have this issue, as it seems to be a common issue/request.

@scottmascio2115
Copy link
Author

Ok will do thanks for the timely response.

@scottmascio2115
Copy link
Author

scottmascio2115 commented Sep 6, 2016

@unixmonkey I am going down the route you suggested and create a pdf on a page by page basis and stitch them together. I am trying to determine how to know when page 1 ends and page 2 begins.

I am using the pdf_from_string method similar to pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')

I know I cant do pdf.first_page so I am thinking I would have to do

pdf_page_one = WickedPdf.new.pdf_from_string('Page one content')
pdf_page_two = WickedPdf.new.pdf_from_string('Page two content')
pdf_page_three = WickedPdf.new.pdf_from_string('Page three content')

Then stitch those three pdf's together. Is there a way to know the max content on page one so a user only has to fill out one form and then somehow that gets parsed into three pdf's. I guess I could do 'page one content'[0,100] and only grab the first 100 words or something similar and then words 100 - 200 on the next page if present.

Thanks again.

@unixmonkey
Copy link
Collaborator

Given what you're telling me, I think I might try to do something like this, where you create two versions of the PDF, one with a large footer on every page, and one with a normal footer.

Then using PDFtk, split out the first page (and discard the rest) of the first PDF, then do the same with the body PDF, but discard the first page something like this:

Warning: this is untested, not very secure, very inefficient, has no error-handling, and may not even work at all.

html_content = render_to_string
cover_pdf = WickedPdf.new.pdf_from_string(html_content, { footer: { margin: { bottom: 200 })
body_pdf = WickedPdf.new.pdf_from_string(html_content, { footer: { margin: { bottom: 10 })

cover_src_temp_file = Tempfile.new(['cover_src', '.pdf'])
cover_src_temp_file.binmode
cover_src_temp_file.write(cover_pdf)
cover_src_temp_file.rewind
cover_temp_file = Tempfile.new(cover_pdf)
`pdftk #{cover_src_temp_file} cat 1 output #{cover_temp_file.path.to_s}` # first page only

body_src_temp_file = Tempfile.new(['body_src', '.pdf'])
body_src_temp_file.binmode
body_src_temp_file.write(cover_pdf)
body_src_temp_file.rewind
body_temp_file = Tempfile.new(body_pdf)
`pdftk #{body_src_temp_file.path} cat 2-end output #{body_temp_file.path}` # everything else

output_temp_file = Tempfile.new(['output', '.pdf'])
`pdftk #{cover_temp_file.path} #{body_temp_file.path} cat output #{output_temp_file.path}`
send_file output_temp_file, disposition: 'inline'

[cover_src_temp_file, body_src_temp_file, cover_temp_file, body_temp_file, output_temp_file].each do |tf|
  tf.close
  tf.unlink
end

If you have trouble getting your HTML to break at a good spot, you can use this CSS class to delimit where your first page content ends:

page 1 content
<div style="page-break-after: always;"></div>
page 2 content

This is kind of a lot of hoop-jumping though, and I might be tempted to give this a shot using prawn for finer-grained control (at the expense of being able to use existing markup and styles and such) for simpler reports.

Let me know how it goes.

@scottmascio2115
Copy link
Author

Thanks for the code! Ill let you know what I come up with

@scottmascio2115
Copy link
Author

@unixmonkey sorry if this is a newbie question but out of curiosity why am I not able to set the margin on a page by page basis using JS. I was able to specify which footer shows up on each page using JS as well as change the background color on the footer. Is it not possible because the size of the header, body and footer content is fixed and cant be overwritten? Also could you recommend any good books or tutorials on pdftk if that is the path I am going to go down I would like to know what its doing. Thanks again you have definitely gone out of your way to help.

@raihanraffique
Copy link

I need to add margin and padding with respect to header to multiple pages pdf report generated with wicked pdf but I can't find any solutions yet, can anyone help me please!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants