如果图像超出了边界框的尺寸,我想剪辑图像。就像CSS溢出一样:隐藏会做到这一点。例如
pdf.grid([0, 0], [3, 27]).bounding_box do
pdf.image image_file
end
现在,如果它比它大,那么这个图像会溢出边界框。当图像超出边界框时,有没有办法剪切图像。 ?我知道使用text_box时文本是可行的。
答案 0 :(得分:1)
你可以设置图像的大小或使图像按比例缩放,使其在保持比例的同时适合某个区域,不相信你可以裁剪图像。
如果您的页面不是动态的,那么图像区域将始终相同,这应该没问题。
pdf.image“image_file_path_& _name”,:position => :center,:fit => [100450];
这基于v0.8.4。
答案 1 :(得分: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是最新版本。