pythonpyfpdf

How to adjust pictures alignment on fpdf


I have this code that generates a investment summary and some analytic images

def investment_summary(pdf, text, bullet_indent=15):
    pdf.set_font("Arial", size=8)

    for point in text.splitlines():
        if point.startswith("-"):
            pdf.set_x(bullet_indent)
        pdf.multi_cell(0, 5, point)

def add_analytics(pdf, image_paths):
  for image in image_paths:
    pdf.image(image, w=180, h=100)  # Adjust width (w) and height (h) as needed
    pdf.add_page()  # Start a new page for each graph

def create_report(county_and_state, llm_text, analytics_location_path=None):
    
    pdf = FPDF()
    pdf.add_page()
    
    pdf.set_text_color(r=50,g=108,b=175)
    pdf.set_font('Arial', 'B', 18)
    
    
    pdf.cell(w=0, h=10, txt="Verus-AI: " + county_and_state, ln=1,align='C')
    
    
    pdf.set_font('Arial', 'B', 16)
    pdf.cell(w=0, h=10, txt="Investment Summary", ln=1,align='L')
    
    investment_summary(pdf, llm_text)

    pdf.set_text_color(r=50,g=108,b=175)
    pdf.set_font('Arial', 'B', 16)
    pdf.cell(w=0, h=10, txt="Analytics", ln=1,align='L')
    add_analytics(pdf, analytics_location_path)
    
    
    
    
    
    
    pdf.output(f'./example1.pdf', 'F')

The issue is the add_analytics function makes new pages for each image. I would like these images to use as much of the space as possible while still being nice to see from the users perspective.

This is what the output looks like enter image description here

I changed my code to add this function

def add_analytics1(pdf, image_paths):
    size_of_image = 150
    for image in image_paths:
        pdf.image(image, w=size_of_image, x=((pdf.w-size_of_image)/2))
        pdf.ln(h=2.5)

but now I am getting this

enter image description here

How do I make sure the first picture does not slightly shift to the left?


Solution

  • you can add an image and also center that image, and also add a space after each image, using this code :

    I was using this library:

    pip install fpdf2

    
    from fpdf import FPDF
    
    size_of_image = 150
    
    
    pdf = FPDF()
    pdf.add_page()
    pdf.set_margin(5.0)
    
    
    
    pdf.image("ex.png", w=size_of_image, x=((pdf.w-size_of_image)/2))
    pdf.ln(h=2.5)
    
    pdf.image("ex.png", w=size_of_image, x=((pdf.w-size_of_image)/2))
    pdf.ln(h=2.5)
    
    pdf.image("ex.png", w=size_of_image, x=((pdf.w-size_of_image)/2))
    pdf.ln(h=2.5)
    
    
    pdf.output('final.pdf')
    
    

    in this code we specify the width only, to be clear,

    because if you added the width and the height, then in that time you are forcing your image to fit that space even if it is smaller or bigger,

    this will reduce the quality,

    in my code, I added the width only, for that the height will be specified auto by the library depending on the height of the image.

    and this will lead to adding a clear image.

    and this is the result :

    enter image description here

    The new problem is :

    that you are using an old version of FPDF :

    this is what i got when i had run your code :

    enter image description here

    it says that your code has a lot that have been changed and removed from the new version , and are available in the old version that you use.

    just update your version.