Background images using prawn on all pages
Asked Answered
O

2

6

I have this code in the view

prawn_document(:page_size=> "A4", :top_margin => 80, :bottom_margin => 40,
               :background => "public/uploads/1.png") do |pdf|

  creation_date = Time.now.strftime('%d-%m-%Y')
  posts = @posts.each do |post|
    pdf.pad(10) do
      pdf.text post.title
      pdf.text post.text
    end
  end

  pdf.page_count.times do |i|
    pdf.go_to_page(i + 1)
    pdf.draw_text "Creation Date : " + creation_date, :at => [200, 780]
    pdf.draw_text "Page : #{i + 1} / #{pdf.page_count}", :at => [450, -3]
  end
end

This gives me the following output:

enter image description here

and this

enter image description here

As you can see in the first image, the image is not centered.

On the 2nd image you can see the image doesn't fit the full page.

I also tried adding the image to each page,the way I added the page number, but here the image overlaps text. But here I can position the image the way I want which works but only that it overlaps the text.

If I put in the header I cannot position and size the image.

So I need help getting the image to fit the page. The image is 700px x 700px.

I also tried using PDFkit, but couldn't get page numbers on that, I feel I am almost there using Prawn, but just this image. Even a different solution will be much appreciated.

Olio answered 26/2, 2014 at 5:6 Comment(0)
A
3

Prawn cannot seem to stretch a background image to fit the page via an option, unfortunately. However, you can. The page the image needs to fit is static, it never changes size. Moreover, we know the exact dimensions:

p Prawn::Document::PageGeometry::SIZES['A4']
# => [595.28, 841.89]

Thus, the best solution is to use an image editor of your choice and create an image of that size. That is, create a canvas of that size and fit your original image whichever way you want. You can choose to stretch it, although I would probably just proportionally shrink it to fit the width and center it vertically. But that's up to you. Just make sure to create a 595px x 842px image and it should fit nicely.

Aframe answered 28/2, 2014 at 9:29 Comment(3)
You can also use the page geometry information along with the image size to come up with a scaling ratio, and then pass :background_scale option. I'd be happy to consider merging a patch that adds support for fitting to a page, though!Cherubini
@GregoryBrown Oh, I did not know that option existed! I don't use Prawn very often so I Googled the API of Document, and apparently ended up with the 0.11 version which did not mention that option at all. It's the first result for 'prawn document api'. The current version does (prawnpdf.org/api-docs/Prawn/Document.html). I would think adding a :background_fit option is nice. Users don't generally like to do math and figure out the proper ratio by themselves. If you have time to spare you could also consider adding positioning options like :center to center an unscaled background.Bocock
I don't have time to spare for pretty much anything except reviewing incoming pull requests, cutting releases, and occasionally fixing bugs. But a patch is welcome for this and any other rough spots you find in Prawn.Cherubini
A
1

Consider using Prawn templates. This will give you an option to style the background and any repeated details (header, footer) easily (using any design software, e.g Illustrator) then in your code you will simply point Prwan to the template file (templeate.pdf)

prawn_document(:page_size=> "A4",:top_margin => 80,:bottom_margin => 40,:template => 'public/pdf/template.pdf') do |pdf|
# Yout pdf building code here
end

That's said, this solution might not be the best performance wise, specially if you are using many PNGs (check this issue)

Another warning to come with this workaround that Prawn team decided to extract the templates feature out of the main gem to a seperate one. You shouldn't worry about this if you are using an old version but if you are using the latest versions you might want to make sure this feature is maintained and not dead.

Astronaut answered 1/3, 2014 at 6:46 Comment(4)
I'm Prawn's maintainer. A couple points: 1) recent versions of Prawn have MUCH faster PNG support, and 2) templates have been extracted because their implementation is fundamentally flawed, so they're unsafe to use on all versions of Prawn. The potential for your documents to become corrupt exists in all released versions.Cherubini
@GregoryBrown thanks for the clarification on the templates point, the thing is templates saved me and probably many others a lot of time that could be wasted drawing complex headers and footers. are there any workarounds i am missing here ?Astronaut
There aren't any workarounds, although the hope is that as we stabilize Prawn there will be more and more plugins for it to make it possible to do various tasks at a much higher level. The issue with templates is that we parse the template files at the lowest level and simply merge them into Prawn's output without actually knowing anything about what is in your templates. For that reason, corruption will happen whenever the operations you're doing in your Prawn code conflict with what's happening in your templates.Cherubini
aha, i see what you mean. I hope i get to contribute into some plugin soon since I've been playing with Prawn a lot lately and its a nice and useful gem indeedAstronaut

© 2022 - 2024 — McMap. All rights reserved.