未发现 load_resource 函数是 FPDF 的一个类方法[英] load_resource function not found as a class method of FPDF

本文是小编为大家收集整理的关于未发现 load_resource 函数是 FPDF 的一个类方法的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在查看以下问题的答案: insert base64 image base64映像PDF使用pyfpdf

这里建议的答案是覆盖现有的load_resource方法.

我所做的是

class EnhancedPdf(FPDF):

    def load_resource(self, reason, filename):
        if reason == "image":
            if filename.startswith("data"):
                f = filename.split("base64,")[1]
                f = base64.b64decode(f)
                f = BytesIO(f)
                return f
            else:
                return super().load_resource(reason, filename)

但是,pycharm突出显示了超级呼叫,并带有消息"未解决的属性参考" for类" load_resource" for类" fpdf"

在我的命令行中,我运行了命令

from fpdf import FPDF
   dir(FPDF)

检查此列表,我看到load_resource函数确实不是列出的方法.因此,我的问题是为什么load_resource函数不可见?

推荐答案

很可能您正在使用python 3.x,其中x> = 5.

在PYPI上说该模块仅对python 3.y有实验支持,其中y <=4.

尝试使用Python 2.7,它可能起作用.

ps:最好尝试 https://pypi.org/project/project/fproject/fpdf2/,更新后的版本.有关错误或问题,请参见 https://github.com/alexanderankin/alexanderankin/pyfpdf ..

.

.

如果您真的想使用旧版本,则可以从原始回购中安装想要的任何版本

pip install git+https://github.com/reingart/pyfpdf@<branchname of tag or commit> 

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

问题描述

I am looking at the answer to the following question: Insert Base64 image to pdf using pyfpdf

The answer suggested here was to override the existing load_resource method.

What I did instead was

class EnhancedPdf(FPDF):

    def load_resource(self, reason, filename):
        if reason == "image":
            if filename.startswith("data"):
                f = filename.split("base64,")[1]
                f = base64.b64decode(f)
                f = BytesIO(f)
                return f
            else:
                return super().load_resource(reason, filename)

However, Pycharm highlights the super call with the message "Unresolved attribute reference "load_resource" for class "FPDF"

In my command line, I ran the commands

from fpdf import FPDF
   dir(FPDF)

Inspecting this list, I see load_resource function is indeed not a listed method. Hence my question is why is the load_resource function not visible?

推荐答案

Most probably you are using Python 3.x where x >= 5 .

On the pypi it says that the module has only experimental support for python 3.y where y <= 4 .

Try it with python 2.7 and it might work.

PS: Better try https://pypi.org/project/fpdf2/, the updated version. For bugs or issues see https://github.com/alexanderankin/pyfpdf .

If you really want to use the old version, you can install whatever version you want from the original repo like this

pip install git+https://github.com/reingart/pyfpdf@<branchname of tag or commit>