USING IRONPDF FOR PYTHON

How to Generate A PDF File in Python

Updated February 18, 2024
Share:

Introduction

In the ever-evolving field of software development, creating and manipulating PDF (Portable Document Format) files is a common requirement. Python, being a versatile and powerful programming language, offers various libraries for handling PDF generation tasks. One such library is IronPDF, a comprehensive tool that simplifies PDF generation in Python.

In this article, we will explore IronPDF for Python, its features, the prerequisites for using it, and step-by-step instructions to generate PDFs in a Python project using IronPDF.

Python PDF Generation Tutorial

  1. Install IronPDF for Python Library
  2. Instantiate ChromePdfRenderer Object
  3. Generate a PDF document using an HTML string
  4. Generate a PDF Document using an HTML file
  5. Generate a PDF Document using a URL

IronPDF - Introduction and Features

IronPDF is a Python library that helps to create PDF documents, and edit or manipulate any existing PDF files within Python applications.

It provides a simple yet powerful API to create PDF files with features like text formatting, images, tables, and more.

With IronPDF, developers can seamlessly integrate PDF functionality into their Python projects, making it an ideal choice for a wide range of applications, including reports, invoices, and documentation.

Key Features of IronPDF:

  • Easy Integration: IronPDF seamlessly integrates with popular Python development environments, making it accessible to developers using tools like PyCharm.
  • Rich Text Support: It supports rich text formatting, allowing developers to create visually appealing PDF documents with ease.
  • Image Handling: IronPDF enables the inclusion of images in PDFs, providing flexibility in designing and customizing documents.
  • Cross-Platform Compatibility: IronPDF works across different platforms, ensuring that generated PDFs can be viewed and interacted with consistently.
  • Document Settings: IronPDF offers robust document settings, empowering users to control PDF metadata, set permissions and passwords for enhanced security to generate encrypted PDF file, and seamlessly integrate digital signatures for document authenticity and integrity.

Prerequisites

Before diving into the PDF generation process using IronPDF, ensure that you have the following prerequisites in place:

  1. Python Installed: Python programming language needs to be installed on your system. You can download and install the latest version from the official Python website (https://www.python.org/).
  2. PyCharm IDE: Use PyCharm or any other Python IDE of your choice. PyCharm is a popular integrated development environment that provides a comfortable workspace for Python development.
  3. IronPDF: IronPDF library downloaded from here or installed using PIP(Python Package Manager). .NET runtime is also required to successfully use IronPDF functionality. Linux, Mac, and Windows users can download the .NET 6.0 version from here.

Create Python Project in PyCharm

Once the prerequisites are met, open PyCharm and create a new Python project. Set up a virtual environment for your project to manage dependencies effectively.

  1. Click on File > New Project.
  2. In the "New Project" window:
    • Enter a name for your project in the "Location" field.
    • Choose the location where you want to save your project files.
  3. Under "Project Interpreter," select the Python interpreter you configured in Step 3.
  4. Choose the project type. For a simple Python project, you can stick with the default settings.

    How to Generate A PDF File in Python: Figure 1

  5. Click Create to create the project.

Install IronPDF for Python Using PIP

To install IronPDF, use the following PIP command in your project's terminal or command prompt:

 pip install ironpdf

This command will automatically download and install the IronPDF library along with its dependencies.

How to Generate A PDF File in Python: Figure 2

Steps to Generate PDF in Python

Now that IronPDF is installed, we'll explore three common scenarios for PDF generation using IronPDF: from an HTML string, an HTML file, and a URL.

1. Generate PDF using an HTML String

IronPDF allows you to generate PDFs from HTML. For creating PDF files from an HTML string, you can follow these simple steps:

from ironpdf import *      
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
# Export to a file or Stream
pdf.SaveAs("output_htmlstring.pdf")
from ironpdf import *      
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")
# Export to a file or Stream
pdf.SaveAs("output_htmlstring.pdf")
#Instantiate Renderer
#Create a PDF from a HTML string using Python
#Export to a file or Stream
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'from ironpdf import * renderer = ChromePdfRenderer() pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>") pdf.SaveAs("output_htmlstring.pdf")
VB   C#

This code initializes the ChromePdfRenderer, renders an HTML string, and the PDF document created.

You can also handle more complex scenarios by including external HTML assets such as images, CSS, and JavaScript:

# Advanced Example with HTML Assets
# Load external html assets: Images, CSS and JavaScript.
# An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", r"C:\site\assets")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
# Advanced Example with HTML Assets
# Load external html assets: Images, CSS and JavaScript.
# An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", r"C:\site\assets")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
#Advanced Example with HTML Assets
#Load external html assets: Images, CSS and JavaScript.
#An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", r"C:\site" + ChrW(7) + "ssets") myAdvancedPdf.SaveAs("html-with-assets.pdf")
VB   C#

Output PDF File

After executing the HTML String to PDF code, you will find a file named output_htmlstring.pdf in your project directory, containing the generated PDF document.

How to Generate A PDF File in Python: Figure 3

2. Generate PDF using an HTML File

Creating a PDF from an existing HTML file is straightforward. Here's an example:

from ironpdf import *      
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Export to a file or Stream
pdf.SaveAs("output_htmlfile.pdf")
from ironpdf import *      
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("example.html")
# Export to a file or Stream
pdf.SaveAs("output_htmlfile.pdf")
#Instantiate Renderer
#Create a PDF from an existing HTML file using Python
#Export to a file or Stream
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'from ironpdf import * renderer = ChromePdfRenderer() pdf = renderer.RenderHtmlFileAsPdf("example.html") pdf.SaveAs("output_htmlfile.pdf")
VB   C#

This snippet uses the ChromePdfRenderer to render an HTML file ("example.html") using the RenderHtmlFileAsPdf method.

Output PDF File

After executing the above code, you will find a file named output_htmlfile.pdf in your project directory, containing the generated PDF document.

How to Generate A PDF File in Python: Figure 4

3. Generate PDF using a URL

Generating a PDF from a URL or a local file path is also possible with IronPDF:

from ironpdf import *      
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")
# Export to a file or Stream
pdf.SaveAs("output_url.pdf")
from ironpdf import *      
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/")
# Export to a file or Stream
pdf.SaveAs("output_url.pdf")
#Instantiate Renderer
#Create a PDF from a URL or local file path
#Export to a file or Stream
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'from ironpdf import * renderer = ChromePdfRenderer() pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/python/") pdf.SaveAs("output_url.pdf")
VB   C#

In this case, the code renders the content of the specified URL ("https://ironpdf.com/python") into a PDF and saves the output file as "url.pdf."

Output PDF File

After executing the Python program, you will find a file named output_url.pdf in your project directory, containing the generated PDF document.

How to Generate A PDF File in Python: Figure 5

With these examples, you can leverage IronPDF to cover a range of scenarios for PDF generation in your Python projects. Whether it's simple HTML strings, existing HTML files, or content from URLs, IronPDF provides a seamless and efficient solution for your PDF generation needs.

For more information on how to generate PDF files, and manipulate and configure different options, please visit this code examples and documentation pages.

Conclusion

IronPDF simplifies PDF generation in Python, offering a feature-rich and easy-to-use library for developers. In this article, we explored the introduction and features of IronPDF, the prerequisites for using it, and a step-by-step guide to generating PDFs in a Python project.

By leveraging IronPDF, developers can enhance their applications with robust PDF functionality, opening up possibilities for creating sophisticated and professional-looking documents.

< PREVIOUS
How to Use Python to Create PDF Reports
NEXT >
How to Create PDF Files with Text and Images in Python

Ready to get started? Version: 2024.6 just released

Free pip Install View Licenses >