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

查看:337
本文介绍了FPDF在Python中产生空白页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了Python脚本,该脚本将基于带有JPEG的文件夹生成PDF.没什么好看的:

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

但是,此代码导致PDF中的每隔一页为空白.有趣的是,这段代码:

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.

我没有看到明显的解决方案-看起来有点像fpdf库.还是不是?

I don't see obvious fix for this - looks a bit like a fpdf library. Or is it not?

推荐答案

请尝试在fpdf中禁用自动分页符模式.为此,请致电set_auto_page_break(0). 我遇到了这个问题,这是我的解决方案.希望对您有帮助!

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)

有关更多信息,请查阅最新文档: http://pyfpdf.readthedocs.io/en/latest/reference/set_auto_page_break/

For more information, please consult the latest documentation: http://pyfpdf.readthedocs.io/en/latest/reference/set_auto_page_break/

这篇关于FPDF在Python中产生空白页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆