New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Images dont respect margins? #1156
Comments
Hi, I successfully created a minimal reproduction of the error class PDF(FPDF):
def __init__(self, orientation="P", unit="mm", format="A4"):
super().__init__(orientation=orientation, unit=unit, format=format)
self.set_auto_page_break(auto=True, margin=15)
def print_images(self, images, fixed_width):
image_info = None
# Iterate through images
for image_path in images:
# Display image
image_info = self.image(image_path, x=self.get_x(), y=self.get_y(), w=fixed_width)
# Update y position to below the image + margin
self.set_y(self.get_y() + image_info.rendered_height + 5)
# Create a new PDF object
pdf = PDF()
# Set up the document
pdf.set_title('My PDF Document')
pdf.set_author('John Doe')
pdf.add_page()
image_path = 'settings/meter_reading_example.png'
images_list = [image_path] * 30
pdf.print_images(images_list, fixed_width=100)
# Output the PDF
pdf.output('test.pdf') |
Seems like I've pinpointed the issue a bit further down: def print_images(self, images, fixed_width):
image_info = None
# Iterate through images
for image_path in images:
# Display image
self.set_x(self.l_margin)
self.image(
image_path, w=fixed_width
)
self.ln(5) Seems like providing custom in # Flowing mode
if y is None:
self._perform_page_break_if_need_be(h)
y = self.y
self.y += h
if x is None:
x = self.x It seems like when for image_path in images:
if (max_per_row != 0 and images_in_row == max_per_row) or (
self.get_x() + fixed_width > self.epw
):
self.set_x(self.l_margin) # Reset x position
self.ln(vertical_spacing)
images_in_row = 0 # Reset images in row counter
# Display image
image_info = self.image(
image_path, w=fixed_width
)
# Update current_x and images_in_row for next iteration
self.set_x(
self.get_x() + fixed_width + 10
) # Add 10 units spacing between images
images_in_row += 1 Respects the margin, but without specifying the But if I were to pass the |
Well done in finding the root cause of your problem!
I see... from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.set_y(pdf.h - 50)
pdf.image('test/image/image_types/insert_images_insert_jpg.jpg')
pdf.output('issue_1156.pdf') |
So this is what I had before, with my Now I did what you suggested, with some small tweaks to work with my "max_per_row" system, here I did some debugging, now look at this: This is the first iteration, the Then second iteration, the The Which seems to be caused by the following line: # self.get_x() gives correct value: 70.0
if images_in_row > 0:
self.set_y(self.get_y() - image_info.rendered_height) # Adjust y-coordinate
# self.get_x() gives incorrect value after this call above: back to 10.0
else:
self.set_y(self.get_y()) # Set y-coordinate as it is for the first image Apparently setting So preserving the This is the final working code: https://pastebin.com/mHfaBD36 |
So in short:
In order to have custom correct_x = self.get_x()
if images_in_row > 0:
self.set_xy(correct_x, self.get_y() - image_info.rendered_height)
I am fairly sure we would |
Other options that you have:
I agree that it can be unexpected, but this is the behaviour since
Thank you for the detailed analysis, but most of the debugging analysis you shared is about your own code (the
I am not so sure. However, I agree that this behaviour should be better documented. What do you think about this @joren-dev? |
I made the minor docstring improvement in PR #1179 Unless you have other comments or questions @joren-dev, we will probably close this issue. |
Discussed in #1155
Originally posted by joren-dev May 4, 2024
So I am using the
self.image
on a page, and while doing so I ran into the following problemIt clearly does not respect the bottom margin that I've said like so in the ctor:
now the images are rendered as follows:
Now for both the width and the height I make sure that itll fit within the margins, is this intended? I would think that for the bottom margin it would auto break like with
self.cell
,self.tables
, etc.I solved the issue myself by adding:
Prior to calling
image_info = self.image(image_path, x=current_x, y=current_y, w=fixed_width)
which solved the issue.However having to do something alike for each image would be pretty inefficient and I have a feeling I am doing something wrong which causes my behavior, could someone help me solve this mystery?
I've tried to make a minimal working example:
But it seems to respect the margins just fine:
So I am doing something wrong, just not sure what.
The text was updated successfully, but these errors were encountered: