大虾:css像溢出:隐藏以包围图像边框

问题描述 投票:2回答:2

如果图像超出边界框的尺寸,我想裁剪图像。就像CSS如何溢出:隐藏会做到这一点。例如

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

现在,如果此图像大于该图像,它将在边界框外溢出。当图像超出边界框时,有什么方法可以裁剪图像。 ?我知道使用te​​xt_box时可能会显示文字。

ruby-on-rails ruby pdf prawn prawnto
2个回答
1
投票

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

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

pdf.image“ image_file_path _&_ name”,:position =>:center,:fit => [100,450];

这是基于v0.8.4的。


0
投票

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

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方法,但是分别跳过了图像尺寸和缩放比例的计算。

这不是完全干净的解决方案。如果您找到更好的,请及时与我联系。

尽管您要记住,此代码段利用了一些实现细节,这些细节不是Prawn的公共API的一部分,并且可以随时更改。

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

© www.soinside.com 2019 - 2024. All rights reserved.