I want to insert some lengthy images (each image can extend to 2 pages) using python fpdf. But the images don't continue to next page once a page break is triggered. How can I print the rest of the images into the next page starting from a given set_y value (header included)?
Asked
Active
Viewed 2,197 times
1
-
you can resize image as page size. So image will be in single page. You cannot eliminate page break. – TheUnknown Nov 15 '17 at 13:27
-
Image quality matters. Currently I'm trying to split the images and insert but there are lot of bugs and effecting performance. Anyways answer with a code example can help. – Uchiha Madara Nov 15 '17 at 13:31
3 Answers
3
Although I believe there is a way with fpdf
, I suggest you switch to pdfkit / wkhtmltopdf based solution.
I have many similar issues with reportlab
, which is very like fpdf
, then switched to wkhtmltopdf, feels much better.
With wkhtmltopdf, you could control layout with standard web stack, use CSS and javascript, use a favorite template engine, then convert HTML file to pdf, easier and much more powerful.

georgexsh
- 15,984
- 2
- 37
- 62
-
can't switch because I've done the entire template in fpdf (almost 300 lines). – Uchiha Madara Nov 15 '17 at 13:35
-
we have migrated a ~2500 lines reportlab based project to wkhtmltopdf, but code lines count does not equal to maintenance difficulty, HTML/CSS is much much easier to debug with. – georgexsh Nov 15 '17 at 13:42
0
If your pdf-dataset is a data string you can start searching for the "\r
" and/or (multiple) '\n
' character(s) using the below code:
chrs = ['\r', '\n', '\n\n']
for item in chars:
pdf-data.replace(item)
In case its formatted strings in a list:
list = pdaf-data
for list-item in list:
for list-item in chars:
pdf-data.replace(item)
In case you read directly from a file use "open file, etc" and readline()
.

ZF007
- 3,708
- 8
- 29
- 48
-1
Just an example of my above comment,
from fpdf import FPDF
from PIL import Image
pdf = FPDF()
pdf.add_page()
im = Image.open('1.jpg')
width_img, height_img = im.size
#mm = (pixels * 25.4) / dpi
# - 25.4 millimeters in an inch
w = (width_img*25.4)/300
h = (height_img*25.4)/300
pdf.image('1.jpg',0,0,w,h)
pdf.output("yourfile.pdf", "F")

TheUnknown
- 66
- 1
- 10
-
1
-
The above code does not handle page break. Its just an example of my previous comment. – TheUnknown Nov 15 '17 at 18:16
-
Then *put that in your answe*. And surely you can partition images to place them on multiple pages. – Martijn Pieters Nov 15 '17 at 18:18
-
-
1Of course you can; a PDF is all about pages. You may have to manually split images, but that’s just coding. – Martijn Pieters Nov 15 '17 at 18:40