Working with Base64 encoding is a common requirement when exchanging files across APIs, embedding documents in JSON or XML, or ensuring binary data is transmitted safely in text-only environments. PDFs are no exception. In this blog post, you will learn how to convert a PDF file to a Base64 string and restore it back to PDF in Python.
We will cover both directions:
- Convert PDF to Base64
- Convert Base64 back to PDF
Let’s dive in with complete code examples and step-by-step explanations.
Why Convert PDF to Base64?
Base64 encoding transforms binary data (like a PDF file) into an ASCII string format. This is especially useful when:
- Embedding PDFs directly in JSON, XML, or HTML.
- Sending PDF data through APIs that only support text.
- Storing PDFs in text-based storage systems like databases.
- Avoiding data corruption during transport.
Aspose.PDF for Python via .NET makes this process simple, reliable, and fast.
Why Use Aspose.PDF for Python to Convert PDF to Base64?
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.
- Document Editing: Add, remove, or modify pages and content.
- Data Extraction: Pull text, tables, images, and metadata with precision.
- Security Features: Apply encryption, watermarks, and digital signatures.
- Cross-Platform Performance: Works reliably on Windows, Linux, and macOS.
Unlike basic Python libraries, Aspose.PDF does not require Adobe Acrobat or any external tool. It is a self-contained solution designed for high-performance applications, making it ideal when you need to convert PDF to Base64 or back from Base64 to PDF in real-world projects.
You can download Aspose.PDF from releases or install it using pip:
pip install aspose-pdf
Convert PDF to Base64 in Python
You can easily load an existing PDF document, optionally add a page, and encode it into a Base64 string with Aspose.PDF for Python.
Please follow the steps below to convert a PDF file into a Base64 string:
- Load a PDF file using the
Documentclass. - Save the file in a memory stream using
io.BytesIO. - Convert memory stream to a byte array.
- Encode the byte array in a Base64 string using the built-in
base64.b64encode()method. - Print the Base64 string or pass it to your API/database.
The following code example shows how to convert a PDF to a Base64 string using Python:
Create a New PDF and Convert It to Base64 in Python
In addition to loading existing files, Aspose.PDF for Python allows you to create new PDF documents from scratch. Once the PDF is generated, you can easily encode it into a Base64 string. This is useful when you need to dynamically generate documents (such as invoices, reports, or receipts) and then send them through APIs or store them in text-based systems.
Follow the steps below:
- Create a new PDF document using the
Documentclass. - Add a new page
- Insert text content with
TextFragment. - Save the PDF to a memory stream.
- Convert to bytes and then encode into a Base64 string.
- Show or use the Base64 string as needed.
This approach is especially powerful when you generate PDFs programmatically and need to pass them immediately as Base64 without saving them to disk.
Convert Base64 to PDF in Python
Once you have a Base64 string, you may want to restore it back into a PDF file. Aspose.PDF makes decoding equally easy.
Please follow the steps below to convert a Base64 string to a PDF document:
- Load the encoded string (e.g., from a file or API).
- Convert the string into raw PDF bytes.
- Create a PDF document using the
Documentclass object with the decoded bytes. - Export the document as a standard
.pdffile.
The following code example demonstrates how to convert a Base64 string back to a PDF document using Python:
Get a Free License
You can try all the features of Aspose.PDF for Python via .NET without limitations by applying a free temporary license. Visit the temporary license page to obtain one and unlock the full potential of the API.
Convert PDF to Base64 Online for Free
If you want to quickly convert a PDF file to Base64 without writing any code, you can use the free online PDF to Base64 converter. This tool runs directly in your browser, so you do not need to install any software or libraries.

Additional Resources
Use these resources to deepen your knowledge and explore real-world scenarios with Aspose.PDF:
Conclusion
In this article, you learned how to convert a PDF file into a Base64 string in Python and then decode the Base64 string back into a PDF file. By using Aspose.PDF for Python via .NET, you gain a reliable and powerful API that makes it easy to handle encoding and decoding of PDF documents in Python applications. Whether you need to embed PDFs in APIs, store them as text in databases, or transfer them securely across platforms, this library gives you full control over the process.
If you have questions or need help, visit our Aspose.PDF free support forum where our team is always ready to assist you.
