Background images using prawn on all pages

帅比萌擦擦* 提交于 2019-12-04 05:33:27

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!