last day (15 days later) » 

14:49
-1
Q: FPDF/img2pdf appending images by using Python

Oksana KrasiukImagine the main script will be executed and will generate images one by one. I need that when the function will be called, every next image will be APPENDED to the export PDF (every time new page) def append_image_to_pdf(image): pdf = FPDF('L', 'mm', 'A4') pdf.set_auto_page_break(0) ...

Was there a question in there?
@Dave , How continuously append a new image to the same pdf, and every new image should go to the new page of this pdf.
Remove the creation of the PDF and the output and simply call append_image_to_pdf in the main script for each image to be added. The creation of the PDF and eventual output should be done once in the mail script.
@Dave e.g. I called this function one it will create (if not exist) PDF file and add image, then when this function called again, it will use already made PDF then will add another page and add a new picture to the page 2
@Dave THe problem here is that ALL pictures will be overwritten. SO this means that is I call this method 5 times, I will have PDF with 1 page and image number 5
As written it is going to create a new PDF every time it is called, add the image then output the PDF.
14:49
@Dave I would like to have a single function, otherwise, I can create all the images and then append them together at the end....
@Dave so is it no way to combine this logic into a single function? e.g. "keep appending"
As I explained, create the PDF and do the output in your main script NOT in this routine. Simply move the creation and output to your main script. Call the creation before the loop through the images and call the output after all of the images have been added.
15:00
So this is the ONLY way to it?
Why is that a bad thing? It could be considered the normal way of doing things. When you have a repeat a sequence of commands over and over you put them into a function of some sort instead of repeating them over and over. By your very definition of what you are trying to do having your mail script call the routine to add images is the only proper way to do it.
The whole Idea if you think about this..... if this can be wrapped up into a single function, it can be simply reused in the whole project 1000000 times.

But when you adding a wrapper around it with Create PDF and add the appended list of images it's become dependant to it and this can't be use as easy it it potentially could
That's it exactly .... I'm in the midst of a project where I do something very similar ... and I created a function to loop through the possible images that might be available to be included .... it gets call for all 5000+ PDFs being generated
I have done now...

in the main code I have added pngList = []
and the have two functions
It sounds like you are not understanding the process of creating a PDF. Unless you make a function like that accept an arbitrary number of images but, even then, you would still have to loop through the images in your function.
15:12
1) I'm appending generated image to the array

2 ) Pass it through like this:
`
pdf = FPDF('P', 'mm', 'A4')
pdf.set_auto_page_break(0)

for image in pngList:
pdf.add_page()
pdf.image(image,w=190)
os.remove(image)
pdf.output(os.path.join(folder,finalExport), "F")`
OK but you only need the one function .... to actually add the page and image ... but glad to hear that you got it working
So you want a PDF for each image? That's different than what your question was.
I'm just not happy with this solution :D
Regardless, I'm glad you got it working the way you wanted/need it to.
Well I think you made it worse :)
May bhe , but I do not want to have these lines in the main code

pdf = FPDF('P', 'mm', 'A4')


pdf.output(os.path.join(folder,finalExport), "F")
Anyway )) THank you ;)
It's your code
You are welcome

last day (15 days later) »