在所有页面上使用大虾的背景图像 [英] Background images using prawn on all pages

查看:96
本文介绍了在所有页面上使用大虾的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在视图中有此代码

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

这给了我以下输出:

还有这个

如您在第一张图片中所见,该图片未居中.

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.

因此,我需要帮助以使图像适合页面大小.图片为700像素x 700像素.

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

我也尝试使用PDFkit,但是无法获得页码,我觉得我使用Prawn差不多了,只是这张图片.甚至其他解决方案也将不胜感激.

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.

推荐答案

大虾似乎无法通过选项拉伸背景图像以适合页面.但是,您可以.图像需要适合的页面是静态的,它永远不会改变大小.此外,我们知道确切的尺寸:

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]

因此,最好的解决方案是使用您选择的图像编辑器并创建该尺寸的图像.也就是说,创建一个该大小的画布并以您想要的任何方式适合您的原始图像.您可以选择拉伸它,尽管我可能只是按比例缩小它以适合宽度并将其垂直居中.但这取决于您.只需确保创建一个595px x 842px的图片,它就可以很好地容纳了.

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.

这篇关于在所有页面上使用大虾的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆