As @pvg mentioned in the comments, overriding load_resource function with your base64 functionality does the trick.

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)

EDIT :

This is a sample code to insert images into pdf. I commented some instructions in code.

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) 
Answer from Uchiha Madara on Stack Overflow
๐ŸŒ
ItSolutionstuff
itsolutionstuff.com โ€บ post โ€บ how-to-convert-pdf-to-base64-string-in-pythonexample.html
How to Convert PDF to Base64 String in Python? - ItSolutionstuff.com
October 30, 2023 - # Import base64 Module import base64 # Python Convert PDF to Base64 String with open("sample.pdf", "rb") as imageFile: encodedString = base64.b64encode(imageFile.read()) print(encodedString);
๐ŸŒ
Blogger
antipatterns.blogspot.com โ€บ 2025 โ€บ 02 โ€บ python-convert-base64-encoded-pdf-file.html
Python: Convert a base64 encoded PDF file to a list of PIL (pillow) Images
February 10, 2025 - Python: Convert a base64 encoded PDF file to a list of PIL (pillow) Images motivation: generally images are easier to work with than PDF fil...
Discussions

python - Insert Base64 image to pdf using pyfpdf - Stack Overflow
I'm using pyfpdf in python to generate pdf files. I have a Base64 which I want to insert into a pdf file without having to save it as an image in my file system. But the pyfpdf image function only More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to use with Base64string PDF data
I have base64 string of the pdf file(Gmail PDF attachments), i am getting the base64 encoded from the server directly,now i need to convert this base64 to image to display the pdf in next tab of the More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to convert a pypdf reader object into a base64 string - Stack Overflow
I'm trying to develop a simple app in Python Flet for displaying each page of a Pdf file. The code imports the pypdf library for PDF management. The UI consists of a button for loading the first page of the PDF and for skipping to the next page, and of a Flet container whose content is a Flet image. Flet image takes a Base64 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
ruby on rails - Convert base64 pdf string into an image - Stack Overflow
I have tried the approach in this example and the duplicate I still continue to receive a blank image when I render it. I need to generate a png file from this base64 string. The label_string is ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Craftmypdf
support.craftmypdf.com โ€บ using a base64 image in a pdf
Using a base64 image in a PDF | CraftMyPDF Documentation
(i) Make(Formerly Integromat): Use the string function base64 to convert an image to a base64 string. (ii) Zapier: Use Javascript or Python code to convert an image to a base64 string
Top answer
1 of 2
3

As @pvg mentioned in the comments, overriding load_resource function with your base64 functionality does the trick.

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)

EDIT :

This is a sample code to insert images into pdf. I commented some instructions in code.

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) 
2 of 2
0

I've been facing this issue lately and the answer from Uchiha Madara didn't work in my case, so I fixed it in a slightly different way. When I tried it with Uchiha's Method, I the same FileNotFound Error you do if you supply an image without any modification to your code ( without the load_resource function ). Since I really needed a solution and there was no way around, I looked into the module code which can be found in

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

If you look around there for a bit, you notice that the image is imported via the _parsepng function. So, we need to edit this to accept a base64 data string.

Basically, what you need to do to fix it:

In the function, you need to add an elif at the top to check whether the "filename" contains a string indicating it's base64, and you need to import 2 new modules.

Copy & paste this code below the first if-Statement to check for a URL:

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

This just looks for the string which is typical for every base64-encoded image, and if it's there and decodes it.

You need to import the base64 and the io modules at the top of the script, so just do that via

import base64, io

Now just supply your base64 string as the file path like you would do normally, and it should work ( did on my tests with python 3.8 ).

Contact me if you have any questions, I hope I can help some people reading this in the future.

๐ŸŒ
Aspose
blog.aspose.com โ€บ aspose.blogs โ€บ convert pdf to base64 in python โ€” step-by-step guide with aspose.pdf
Convert PDF to Base64 in Python โ€” Step-by-Step Guide with Aspose.PDF
September 5, 2025 - Aspose.PDF for Python via .NET is a powerful PDF processing library that enables developers to create, edit, convert, and secure PDF documents programmatically. It goes far beyond simple parsing by offering enterprise-grade features such as ยท PDF Conversions: Convert PDF to Base64, images, HTML, DOCX, and more.
Find elsewhere
๐ŸŒ
Base64.Guru
base64.guru โ€บ converter โ€บ decode โ€บ image
Base64 to Image | Base64 Decode | Base64 Converter | Base64
Is there an API we can use to do this? I wanted to implement this in my automation where i have a base64 then convert it to image and download. Thanks! ... Hi, I want to do exactly this but in Python. I got my base64 string and I want to convert that into a PIL.Image type.
๐ŸŒ
Pythondex
pythondex.com โ€บ convert-pdf-to-base64-python
Convert PDF To Base64 In Python - Pythondex
January 31, 2024 - import base64 pdf = open('sample.pdf', 'rb') bs64 = base64.b64encode(pdf.read()) pdf.close() print(bs64) Above is the python code to convert pdf to base64, we encode the pdf file using the base64 encode method which will give us the base64 string ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ encoding-and-decoding-base64-strings-in-python
Encoding and Decoding Base64 Strings in Python
September 4, 2023 - Base64 encoding is a popular technique ... cannot process raw binary data but would readily handle text. With Python, we can use the base64 module to Base64 encode and decode text and binary data....
๐ŸŒ
Wondershare
pdf.wondershare.com โ€บ convert-pdf โ€บ convert-pdf-to-base64.html
The Complete Guide To Convert PDF to Base64
Step 1 Go to the Aspose website and select "Upload or Drop your files." Step 2 Navigate to the "Save as" tab and select Base64 as the output format. Step 3 Click the green "Convert" button to initiate the process.
Published: December 28, 2022
๐ŸŒ
Base64.Guru
base64.guru โ€บ home โ€บ developers โ€บ python โ€บ examples
Convert Base64 to PDF in Python | Examples | Python | Developers | Base64
Below I want to show you a basic example of how to do this, but before continuing I want to warn you that PDF files may contain malicious content that may jeopardize the security of users viewing such PDF files. Therefore, if you get Base64 from an untrusted source, you must sanitize the PDF contents by removing all active content (such as JavaScript, actions, embedded files). Full working example (please note that this example was tested only in Python 3.7.3):
๐ŸŒ
Base64
base64.dev โ€บ home โ€บ articles โ€บ base64 in python
Base64 Encoding & Decoding in Python โ€” base64.dev
July 3, 2026 - import base64 # Encode a file to Base64 string with open("image.png", "rb") as f: encoded = base64.b64encode(f.read()).decode("ascii") print(encoded[:80] + "...") # iVBORw0KGgoAAAANSUhEUgAA...
๐ŸŒ
Code Beautify
codebeautify.org โ€บ blog โ€บ how-to-convert-base64-to-image-using-python
How to Convert Base64 to Image Using Python
February 22, 2024 - Remember to install the necessary ... a Base64 string to an image in Python, we can use the base64 module to decode the string and the PIL (Pillow) library to work with images....
๐ŸŒ
Base64.Guru
base64.guru โ€บ home โ€บ base64 converter โ€บ base64 encode
PDF to Base64 | Base64 Encode | Base64 Converter | Base64
It automatically detects the content type of the uploaded PDF file, so that you simply copy the complete result. If you do not know what output format you need, check the following examples to see how will look the result of the same Base64-encoded PDF file formatted in each of the available formats (as an example Base64 string I use first 64 bytes of a PDF file):โ€ข Plain text: