使用pyfpdf将Base64图像插入pdf

Insert Base64 image to pdf using pyfpdf

我在 python 中使用 pyfpdf 生成 pdf 文件。我有一个 Base64,我想将其插入到 pdf 文件中,而不必将其作为图像保存在我的文件系统中。但是pyfpdf图像函数只接受文件路径。

fpdf.image(name, x = None, y = None, w = 0, h = 0, type = '', link = '')

有没有办法(破解)直接从内存中插入 base64 或缓冲图像,而无需事先保存到文件系统中?我什至在 github 上检查了他们的源代码,但无法理解。

link : https://github.com/reingart/pyfpdf/tree/master/fpdf

正如@pvg 在评论中提到的那样,用你的 base64 功能覆盖 load_resource 函数就可以了。

import base64,io

def load_resource(self, reason, filename):
    if reason == "image":
        if filename.startswith("http://") or filename.startswith("https://"):
            f = BytesIO(urlopen(filename).read())
        elif filename.startswith("data"):
            f = filename.split('base64,')[1]
            f = base64.b64decode(f)
            f = io.BytesIO(f)
        else:
            f = open(filename, "rb")
        return f
    else:
        self.error("Unknown resource loading reason \"%s\"" % reason)

编辑:

这是将图片插入pdf的示例代码。我在代码中注释了一些说明。

from fpdf import FPDF
import os
import io
import base64


class PDF(FPDF):

    def load_resource(self, reason, filename):
        if reason == "image":
            if filename.startswith("http://") or filename.startswith("https://"):
                f = BytesIO(urlopen(filename).read())
            elif filename.startswith("data"):
                f = filename.split('base64,')[1]
                f = base64.b64decode(f)
                f = io.BytesIO(f)
            else:
                f = open(filename, "rb")
            return f
        else:
            self.error("Unknown resource loading reason \"%s\"" % reason)


    def sample_pdf(self,img,path):

        self.image(img,h=70,w=150,x=30,y=100,type="jpg")
        #make sure you use appropriate image format here jpg/png
        pdf.output(path, 'F')

if __name__ == '__main__':
    img = # pass your base64 image
    # you can find sample base64 here : https://pastebin.com/CaZJ7n6s

    pdf = PDF()
    pdf.add_page()
    pdf_path = # give path to where you want to save pdf
    pdf.sample_pdf(img,pdf_path) 

我最近一直面临这个问题,宇智波斑的回答对我没有用,所以我用稍微不同的方式解决了它。当我尝试使用 Uchiha 的方法时,如果您提供图像而没有对代码进行任何修改(没有 load_resource 函数),我会遇到同样的 FileNotFound 错误。因为我真的需要一个解决方案而且没有办法,我查看了可以在

中找到的模块代码

C:/Users/user/AppData/Local/Programs/Python/Python38/Lib/site-packages/fpdf/fpdf.py

如果你四处看看,你会注意到图像是通过 _parsepng 函数导入的。因此,我们需要对其进行编辑以接受 base64 数据字符串。

基本上,您需要做些什么来修复它:

在函数中,需要在最前面添加一个elif来检测"filename"中是否包含表示是base64的字符串,需要导入2个新模块。

将此代码复制并粘贴到第一个 if 语句下方以检查 URL:

elif "data:image/png;base64" in name:
                f = name.split('base64,')[1]
                f = base64.b64decode(f)
                f = io.BytesIO(f)

这只是查找每个 base64 编码图像的典型字符串,如果它存在并对其进行解码。

您需要在脚本顶部导入 base64 和 io 模块,因此只需通过

import base64, io

现在只需像平常一样提供您的 base64 字符串作为文件路径,它应该可以工作(在我的 python 3.8 测试中完成)。

如有任何疑问,请与我联系,希望我能帮助以后阅读本文的人。