使用pyfpdf将Base64图像插入pdf

内浦马达拉

我在python中使用pyfpdf生成pdf文件。我有一个Base64,我想将它插入pdf文件,而不必将其另存为文件系统中的图像。但是pyfpdf图片功能仅接受文件路径。

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

有没有一种方法(破解)直接从内存中插入base64或缓冲的图像,而不必事先保存到文件系统中?我什至在github上检查了它们的源代码,无法确定。

链接: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) 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Java将Base64编码的图像上传到Amazon s3

使用HttpUrlConnection Android将base64编码的图像发送到服务器

如何使用JavaScript将图像转换为Base64字符串?

使用ReactiveGridFsTemplate Spring Webflux将base64图像存储在mongodb中

使用Java将图像转换为base64

如何使用JavaScript将data:image / png:base64 ...解码为真实图像

使用Laravel发送Base64图像

在VBA中使用Base64将图像插入到工作表中?

TCPDF并插入编码为base64的图像

将base64 pdf字符串转换为图像

将Base64图像插入数组PHP PDO

使用Symfony和VichUploader将base64图像转换为图像文件

无法使用cassandra驱动程序将base64图像数据存储在Cassandra中

如何使用PutObjectAsync使用dotnet SDK将base64 pdf上传到S3

使用base64编码的数据通过Apps脚本将图像插入Google表格

使用循环解码base64图像

使用PL / SQL将base64图像解码为BLOB

ITextSharp / Pdftk:将Web上的Base64图像作为Pseude-Signature放置在PDF上

使用angularjs将SVG中的xlink:href设置为base64编码的图像

无法使用Webpack将base64图像导入React组件

插入 base64 图像作为 wordpress 帖子附件

无法使用java将图像转换为base64

使用javascript获取base64图像

如何将 PIL 图像数组转换为 base64 格式的 PDF 字符串?

将值 Base64 保存到图像 PNG 使用干预图像 Laravel 存储

转换为base64后如何将画布图像插入数据库

使用 node.js 将 base64 字符串插入博客 mysql

无法使用 docx 将 base64 编码的图像添加到 Word 文档

如何使用 Javascript(使用 base64 数据)将图像保存到 iPhone?