问题
I wrote Python script that will produce PDFs based on folders with JPEGs. Nothing fancy:
import os
from fpdf import FPDF
folders = [ ... here are numbers - folders are numbered ... ]
for folder in folders:
pdf = FPDF()
for fil in os.scandir("parent folder" + str(folder) + "/"):
pdf.add_page()
pdf.image(fil.path, w=210, h=297)
pdf.output("target location/" + str(folder) + ".pdf", "F")
This code however results in PDF having every other page blank. Interestingly enough this code:
import os
from fpdf import FPDF
folders = [ ... here are numbers - folders are numbered ... ]
for folder in folders:
pdf = FPDF()
pdf.add_page()
for fil in os.scandir("parent folder" + str(folder) + "/"):
pdf.image(fil.path, w=210, h=297)
pdf.output("target location/" + str(folder) + ".pdf", "F")
produces file with empty first page and the rest is correct.
I don't see obvious fix for this - looks a bit like a fpdf library. Or is it not?
回答1:
Please try by disabling automatic page break mode in fpdf. To do this, please call set_auto_page_break(0)
.
I faced this problem and this was my solution. Hope it helps!
from fpdf import FPDF
pdf = FPDF()
pdf.set_auto_page_break(0)
For more information, please consult the latest documentation: http://pyfpdf.readthedocs.io/en/latest/reference/set_auto_page_break/
回答2:
The way I solved this was by using the:
pdf.set_auto_page_break(0)
after i initiated the PDF object and then adding a page before every image insertion. The example code is:
from fpdf import FPDF
import os
pdf = FPDF(orientation = 'L') #if you want your pdf to be in Landscape mode
pdf.set_auto_page_break(0)
for i in range(10):
image_location = os.getcwd() + '\\image_' + str(i) + '.jpg'
pdf.add_page()
pdf.image(image_location, w=270) #270 used for A4 paper in Landscape orientation
pdf.output('my_pdf.pdf')
The PDF file that was generated using the above code had no blank pages.
来源:https://stackoverflow.com/questions/40533405/fpdf-produces-empty-pages-in-python