【问题标题】:Prawn: css like overflow: hidden for bounding boxes with images大虾:css like overflow: hidden 用于带有图像的边界框
【发布时间】:2011-02-10 13:37:33
【问题描述】:

如果图像超出边界框的尺寸,我想剪辑它。就像 CSS overflow: hidden 一样。例如。

pdf.grid([0, 0], [3, 27]).bounding_box do
 pdf.image image_file
end

目前,如果此图像大于边界框,则该图像会溢出边界框。当图像超出边界框时,有什么方法可以剪辑图像。 ?我知道在使用 text_box 时这对于文本是可能的。

【问题讨论】:

    标签: ruby-on-rails ruby pdf prawn prawnto


    【解决方案1】:

    您可以设置图像的大小或将图像缩放以使其适合特定区域同时保持比例,不要相信您可以裁剪图像。

    如果您的页面不是动态的,即图像区域将始终相同,这应该可以。

    pdf.image "image_file_path_&_name", :position => :center, :fit => [100,450];

    这是基于 v0.8.4。

    【讨论】:

      【解决方案2】:

      不幸的是,目前似乎没有合适的方法将图像裁剪为边界框。面对这个问题我想出了这个美:

      class SamplePdf
      
        include Prawn::View
      
        def initialize
      
          crop_width = 100 # your width here
          crop_height = 50 # your height here
          image_path = '/path/to/your_image.jpg'
      
          bounding_box [0, 0], width: crop_width, height: crop_height do
      
            pdf_obj, _ = build_image_object(image_path)
      
            x, y = document.send(:image_position, crop_width, crop_height, {})
            document.send(:move_text_position, crop_height)
      
            label = "I#{document.send(:next_image_id)}"
            document.state.page.xobjects.merge!(label => pdf_obj)
      
            cm_params = PDF::Core.real_params([crop_width, 0, 0, crop_height, x, y - crop_height])
            document.renderer.add_content("\nq\n#{cm_params} cm\n/#{label} Do\nQ")
          end
        end
      end
      

      它基本上适应了Prawn::Images#image方法,但分别跳过了图像尺寸和缩放的计算。

      这不是一个干净的解决方案。如果您找到更好的,请随时通知我。

      您应该记住,虽然这个 sn-p 利用了一些不属于 Prawn 的公共 API 的实现细节,并且可以随时更改。

      在撰写本文时,Prawn 2.0.1 是最新版本。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-24
        • 1970-01-01
        • 2018-11-26
        相关资源
        最近更新 更多