I am trying to output different photos into different pdfs but I see that it is still printing the same photo in all pdfs.
namelist=[]
values are ['ABC1 XYZ1', 'ABC2 XYZ2', 'ABC3 XYZ3', 'ABC4 XYZ4', 'ABC5 XYZ5']
pics named same as in namelist
but it still outputs only first image. Why?
ABC2 XYZ2.pdf
see here in ABC2 XYZ2.pdf
. It should output ABC2 XYZ2.jpg image.
from fpdf import FPDF
from main import *
import os
class PDF(FPDF):
def lines(self):
self.set_fill_color(255,99,71) # color for outer rectangle
self.rect(5.0, 5.0, 200.0,287.0,'DF')
self.set_fill_color(255,255,255) # color for inner rectangle
self.rect(8.0, 8.0, 194.0,281.0,'FD')
def imagex(self,sctplt,x,y,width,height):
self.set_xy(x,y)
self.image(sctplt, type='', w=width, h=height)
def titles(self):
self.set_xy(0.0,0.0)
self.set_font('Arial', 'B', 16)
self.set_text_color(220, 50, 50)
self.cell(w=210.0, h=40.0, align='C', txt="t", border=0)
def texts(self,name):
with open(name,'rb') as xy:
txt=xy.read().decode('latin-1')
self.set_xy(10.0,80.0)
self.set_text_color(76.0, 32.0, 250.0)
self.set_font('Arial', '', 12)
self.multi_cell(0,10,txt)
namelist=[]
for k in range(len(x)):
namelist.append(x[k][0])
print(namelist)
pdf = PDF(orientation='P', unit='mm', format='A4')
pdf.add_page()
pdf.lines()
image1="wisdom test/1.png"
image2="wisdom test/2.png"
pdf.imagex(image1,89.0,10.0,2000/50,1920/50)
pdf.imagex(image2,20.0,50.0,2000/50,1920/50)
for i in range(len(namelist)):
currstudent=namelist[i]
pdf.imagex("pics/{}.jpg".format(currstudent),22.0,52.5,1900/60,1850/55)
pdf.output('{}.pdf'.format(currstudent),'F')
pdf.titles()
pdf.output('{}.pdf'.format(currstudent),'F')
Your code is only creating a single PDF file with one page. If you move all of your PDF creation code inside your for i in range(len(namelist)):
loop, you will create a separate PDF file per name in the namelist. Something like:
for i in range(len(namelist)):
pdf = PDF(orientation='P', unit='mm', format='A4')
pdf.add_page()
pdf.lines()
image1="wisdom test/1.png"
image2="wisdom test/2.png"
pdf.imagex(image1,89.0,10.0,2000/50,1920/50)
pdf.imagex(image2,20.0,50.0,2000/50,1920/50)
currstudent=namelist[i]
pdf.imagex("pics/{}.jpg".format(currstudent),22.0,52.5,1900/60,1850/55)
pdf.titles()
pdf.output('{}.pdf'.format(currstudent),'F')
I used some sample images and modified your code and I was able to generate separate PDF files, each with different images.
Pre-script run directory listing:
$ ls . ./images/ ./output
.:
images output script.py
./images/:
one.jpg small-left.jpg small-right.jpg three.jpg two.jpg
./output:
Post-script run directory listing: (now 3 .pdf files in the output directory)
$ ls . ./images/ ./output
.:
images output script.py
./images/:
one.jpg small-left.jpg small-right.jpg three.jpg two.jpg
./output:
one.pdf three.pdf two.pdf
Contents of script.py
from fpdf import FPDF
import os
class PDF(FPDF):
def lines(self):
self.set_fill_color(255,99,71) # color for outer rectangle
self.rect(5.0, 5.0, 200.0,287.0,'DF')
self.set_fill_color(255,255,255) # color for inner rectangle
self.rect(8.0, 8.0, 194.0,281.0,'FD')
def imagex(self,sctplt,x,y,width,height):
self.set_xy(x,y)
self.image(sctplt, type='', w=width, h=height)
def titles(self):
self.set_xy(0.0,0.0)
self.set_font('Arial', 'B', 16)
self.set_text_color(220, 50, 50)
self.cell(w=210.0, h=40.0, align='C', txt="t", border=0)
def texts(self,name):
with open(name,'rb') as xy:
txt=xy.read().decode('latin-1')
self.set_xy(10.0,80.0)
self.set_text_color(76.0, 32.0, 250.0)
self.set_font('Arial', '', 12)
self.multi_cell(0,10,txt)
for name in ["one", "two", "three"]:
pdf = PDF(orientation='P', unit='mm', format='A4')
pdf.add_page()
pdf.lines()
pdf.imagex("images/small-left.jpg",89.0,10.0,2000/50,1920/50)
pdf.imagex("images/small-right.jpg",20.0,50.0,2000/50,1920/50)
pdf.imagex(f"images/{name}.jpg",22.0,52.5,1900/60,1850/55)
pdf.titles()
pdf.output(f"output/{name}.pdf",'F')