当图像超出页面尺寸时,如何为 pyfpdf 自动分页?
我在此处查看了类似的问题,但似乎没有除了切换到不同的库之外,这是一个解决方案。
我创建了一些代码来自动垂直和水平间隔图像,但是当插入的图像大于页面时,我遇到自动分页符问题(因为图像将被截断)。
def generate_pdf_report(start_height):
name = "PDF"
page_width = 210
page_height = 297
object_margins = 10
objects_per_row = 1
objects_in_path = 6 ##SAMPLE VARIABLE
total_margins = object_margins * (objects_per_row + 1)
object_width = (page_width - total_margins)/objects_per_row
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
object_counter = 0
height_position = start_height
for item in range(objects_in_path):
object_counter += 1
position = object_counter % objects_per_row
if position == 0:
start_position = object_width * (objects_per_row - 1) + object_margins * objects_per_row
pdf.image("test_image.jpg", x=start_position, y=height_position, w=object_width)
height_position += start_height + object_margins
else:
start_position = object_width * (position - 1) + object_margins * position
pdf.image("test_image.jpg", x=start_position, y=height_position, w=object_width)
pdf.output('test_pdf.pdf')
是否有某种方法可以返回使用 pdf.image() 插入的每个图像的尺寸?由于我设置了图像的宽度,因此默认情况下高度会自动缩放。因此,目前我必须根据图像的最终高度实际手动设置函数的 start_height
参数,否则它可能会重叠或间隙太大。由于我没有图像高度来充分间隔图像或跳到下一页
我能想到的一个解决方案是测量 pyfpdf 包外部的图像并确定图像的宽高比,所以我可以确定最终图像的高度。但我认为应该有更好更简单的方法吗?
I looked at a similar question here but there doesn't seem to be a solution except to switch to a different library.
I have created some code to automatically space my images vertically and horizontally, but I am having a problem with auto page breaks when the image inserted is larger than the page (since the image will get cut off).
def generate_pdf_report(start_height):
name = "PDF"
page_width = 210
page_height = 297
object_margins = 10
objects_per_row = 1
objects_in_path = 6 ##SAMPLE VARIABLE
total_margins = object_margins * (objects_per_row + 1)
object_width = (page_width - total_margins)/objects_per_row
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
object_counter = 0
height_position = start_height
for item in range(objects_in_path):
object_counter += 1
position = object_counter % objects_per_row
if position == 0:
start_position = object_width * (objects_per_row - 1) + object_margins * objects_per_row
pdf.image("test_image.jpg", x=start_position, y=height_position, w=object_width)
height_position += start_height + object_margins
else:
start_position = object_width * (position - 1) + object_margins * position
pdf.image("test_image.jpg", x=start_position, y=height_position, w=object_width)
pdf.output('test_pdf.pdf')
Is there some kind of way to return the dimensions of each image inserted using pdf.image()
? Since I set the width of the image, the height is automatically scaled by default. So currently I have to actually manually set the start_height
parameter of the function depending on the resulting height of the image--else it can overlap or have too large a gap. Since I don't have the image height to adequately space the images or skip to the next page
There is one solution I can think of which is to measure the image outside the pyfpdf package and determine the width-height ratio of the image so I can determine the height of the final image. But I think there should be a better and easier way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论