【问题标题】:PDF Prawn : Problem with image overriding the textPDF Prawn:图像覆盖文本的问题
【发布时间】:2011-10-24 18:12:28
【问题描述】:

我想在每一页上放一张图片作为背景。这是我的代码:

Prawn::Document.generate("#{Rails.root.to_s}/public/#{filename}.pdf", :page_size => [576,576], :left_margin => 50,:right_margin => 50, :page_layout => :portrait, :skip_page_creation => true, :skip_encoding => true ) do |pdf|

  pdf.repeat :all do
    pdf.image open(APP_CONFIG['site_url']+"/images/pdf/bg_blank_high.jpg"), :at => [-pdf.bounds.absolute_left,  pdf.bounds.absolute_top], :width => 576, :height => 576
  end

  pdf.start_new_page(:margin => [75,50,50,50]) 
  pdf.fill_color "8c8464"
  pdf.text story.contents, :size => 12, :align => :left, :leading => 3 
end 

问题是 - 图像覆盖了所有页面中的文本。

有没有办法在图像上放置文字或使图像“发送回”或任何其他想法?

【问题讨论】:

    标签: ruby-on-rails image pdf background prawn


    【解决方案1】:

    在 Prawn 手册中,他们将此作为文本覆盖的图像的示例:

    # images/absolute_position.rb
    # One of the options that the image method accepts is :at. If you've read some of the graphics
    # examples you are probably already familiar with it. Just provide it the upper-left corner where you
    # want the image placed.
    # While sometimes useful this option won't be practical. Notice that the cursor won't be moved after the
    # image is rendered and there is nothing forbidding the text to overlap with the image.
    
    y_position = cursor
    text "The image won't go below this line of text."
    image "#{Prawn::BASEDIR}/data/images/fractal.jpg", :at => [200, y_position]
    text "And this line of text will go just below the previous one."
    

    所以据我所知,这可能是您构建文档的方式或光标放置的问题。

    【讨论】:

      【解决方案2】:

      似乎重复块中的代码是在文本呈现后调用的,因此将其置于文本'above'。 sn-p 提供的代码有效,因为它只涉及单个图像。

      prawn_document( filename:       "filename.pdf",
                      background:     path_background_image) do |pdf|
      ...
      

      可能对你有用。但它只提供 low_res (72 dpi) 图像。

      有一个请求允许为背景图像设置分辨率: https://github.com/sandal/prawn/pull/292

      【讨论】:

        猜你喜欢
        • 2020-08-24
        • 1970-01-01
        • 2011-04-16
        • 1970-01-01
        • 2021-06-08
        • 2020-12-13
        • 1970-01-01
        • 2011-08-27
        • 2019-06-28
        相关资源
        最近更新 更多