Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
This article will use IronPDF for Python to create simple PDF documents from templates.
IronPDF is a powerful Python library that revolutionizes the way developers interact with PDF documents. Designed to simplify the creation, editing, and manipulation of PDF files, IronPDF empowers Python programmers to effortlessly integrate sophisticated PDF functionalities into their applications. Whether it's generating PDFs from scratch, converting HTML content into high-quality PDFs, or merging, splitting, and editing existing PDFs, IronPDF's comprehensive set of tools and APIs offer an intuitive and efficient solution. With its user-friendly interface and extensive documentation, IronPDF opens up a world of possibilities for developers seeking to harness the full potential of PDFs in their Python projects, making it an invaluable asset in the realm of document management and automation.
Generating a PDF from a template in Python requires the following prerequisites to be in place:
IronPDF Library: To install the IronPDF library, use pip
, the Python package manager. Open your command-line interface and execute the following command:
pip install ironpdf
Here are the steps to create a new Python Project in PyCharm.
To create a new Python project open PyCharm, go to "File" in the top menu and click on "New Project".
PyCharm IDE
A new window will appear where you can specify the project's environment and location. After selecting the environment click on the Create button.
Create a new Python project in PyCharm
To install IronPDF just open the terminal and run the following command pip install ironpdf
and press enter. The terminal output should look like this.
Install the IronPDF package
This section will explain how to generate PDF documents from HTML templates using HTML templates and input data from the console to create PDF files.
Firstly, let's import some dependencies for creating PDF files.
from ironpdf import ChromePdfRenderer
from jinja2 import Template
Next, declare renderer
as a ChromePdfRenderer
object and use it to render HTML templates.
renderer = ChromePdfRenderer()
Now, let's create an HTML template document for reusable purposes to create PDF files. Just create a new variable and populate it with HTML content containing braces at the start and the end.
html_template = """
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>{{ title }}</h1>
<p>
Hello, {{ name }}! This is a sample PDF generated from a template using IronPDF for Python.
</p>
<p>
Your age is {{ age }} and your occupation is {{ occupation }}.
</p>
</body>
</html>
"""
Now that the design template is ready, let's write code that will take input from the user and then store it in the dictionary.
title = input("Enter the title: ")
name = input("Enter your name: ")
age = input("Enter your age: ")
occupation = input("Enter your occupation: ")
data = {
"title": title,
"name": name,
"age": age,
"occupation": occupation
}
Further, the below code will add data into the template document and render HTML templates using the IronPDF renderer
object that was created earlier. At the end, save the PDF file using the SaveAs
method.
template = Template(html_template)
html_content = template.render(**data)
pdf = renderer.RenderHtmlAsPdf(html_content)
pdf.SaveAs("output.pdf")
The code to create PDF files dynamically is completed. Let's run the code to see the output.
After running the code it will ask for the following inputs from the user.
The console requires additional input from user
Just enter them one by one and press enter after each input. Once all the four inputs are entered, it will generate a PDF file.
The output PDF file
Now rerun the program and try different inputs.
The console with different input
As you can see below, the output file format is the same, but it is updated with the new inputs.
The new output PDF file
For more information on how to create, modify and read PDF in Python using IronPDF please visit the documentation page.
In the world of programming and document automation, Python's utilization of the IronPDF library for rendering PDFs documents from templates has revolutionized document management and workflow efficiency. This powerful combination empowers developers to effortlessly create tailored PDF files, such as invoices, reports, and certificates, enhancing productivity and user experience. The seamless integration of IronPDF's comprehensive tools and APIs within Python projects enables developers to handle PDF generation, editing, and manipulation tasks with ease, streamlining the development process and ensuring consistent, polished outputs. Python's versatility, coupled with IronPDF's capabilities, makes this dynamic duo an indispensable asset for any developer seeking efficient and automated PDF document solutions. Also, it is possible to use the same technique to create PDF from CSV file by just editing your program files, in this case Python file.
As you can see the output files are watermarked, you can easily remove them by purchasing a license. The $749 Lite package comes with a perpetual license, a 30-day money-back guarantee, a year of software support, and upgrade possibilities. IronPDF also offers a free trial license.
9 .NET API products for your office documents