FPDF在Python中产生空页[英] FPDF produces empty pages in Python

本文是小编为大家收集整理的关于FPDF在Python中产生空页的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我编写了Python脚本,该脚本将根据带有JPEG的文件夹生成PDF.没什么花哨的:

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")

此代码导致PDF具有空白的其他页面.有趣的是,此代码:

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")

用空的第一页生成文件,其余的是正确的.

我看不到明显的修复 - 看起来有点像FPDF库.还是不是?

推荐答案

请在FPDF中禁用自动页面断开模式.为此,请致电set_auto_page_break(0). 我面对这个问题,这是我的解决方案.希望它有帮助!

from fpdf import FPDF 
pdf = FPDF()
pdf.set_auto_page_break(0)

有关更多信息,请咨询最新文档: a>

其他推荐答案

我解决的方式是使用:

pdf.set_auto_page_break(0)

启动PDF对象后,然后在每个图像插入之前添加页面.示例代码是:

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')

使用上述代码生成的PDF文件没有空白页.

本文地址:https://www.itbaoku.cn/post/2090905.html

问题描述

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?

推荐答案

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/

其他推荐答案

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.