Techno Blender
Digitally Yours.

How to Easily Create a PDF File with Python (in 3 Steps) | by Frank Andrade | Oct, 2022

0 53


How to create PDF files in a few minutes using Python and HTML templates

Image via Shutterstock under license to Frank Andrade

PDF is probably one of the most common file types that we can find on our computers.

We use PDFs for our resumes, reports, invoices, you name it!

A common way to create a PDF file is by saving a Word file as .pdf, but we can also create a PDF file using Python.

The pros? If you plan to create multiple PDFs using the same template, you could do it with Python! But first, we’ll see how to create a PDF file using Python.

In this tutorial, we’ll first create a simple PDF file and then we’ll create a more advanced one that looks like the one below.

Image by author

There are different ways to create a PDF file with Python, but I found using HTML templates the easiest one.

With this option, we can create basic and advanced PDFs in a few minutes!

Install the libraries

To create a PDF file, we need to install the following libraries:

pip install pdfkit
pip install jinja2

Before we use thepdfkit library, we need to install wkhtmltopdf. The steps to install it is up to your operating system:

# macOS (you need to install brew first)
brew install homebrew/cask/wkhtmltopdf
# Ubuntu
sudo apt-get install wkhtmltopdf

In case you’re on Windows, you can download the installer here.

Now let’s create a simple PDF with Python.

Step 1: Create an HTML template with placeholders

First, we need to create an HTML document that we’ll use as a template later.

To create this HTML document, we’ll use a website called HTML Editor. On this website, we can type text in the visual editor on the left and generate its HTML code in the source editor on the right.

Here’s the text I introduced in the editor (this file can be found on my GitHub)

Note the placeholders I created using the {{}}. Later we’ll introduce values inside these curly braces with Python.

Here’s the HTML code that was generated by the HTML editor.

We need to copy this HTML code, create an HTML file in our working directory, and paste the code on it.

Step 2: Create a Python variable for each placeholder

First, we import jinja2 and pdfkit. I have a placeholder named {{today_date}}, so I also import datetime to get today’s date.

Then we create a Python variable for each placeholder in our HTML document and we create a dictionary that pairs the placeholders with the Python variables.

Note that I named each Python variable as the placeholders, but they could have different names.

Step 3: Create an environment for our template and export the PDF

Now it’s time to use jinja2 to create an environment for our template.

template_loader = jinja2.FileSystemLoader(‘./’)
template_env = jinja2.Environment(loader=template_loader)

Then we specify which template we’re using. In my case, I named the HTML file as basic-template.hmtl. After this, we render the dictionary we created in step 2.

template = template_env.get_template(‘basic-template.html’)
output_text = template.render(context)

Next, we add wkhtmltopdf to the pdfkit configuration. To do so, we need to specify the path where wkhtmltopdf was installed.

Here’s the command you need to run to get this path.

# macOS/Linux
>>> which wkhtmltopdf
'/usr/local/bin/wkhtmltopdf'
# Windows
>>> where wkhtmltopdf
'C:\Program Files\wkhtmltopdf'

Finally, we export the PDF file using the .from_string() method.

Note that I added a CSS parameter to the .from_string() method. This is optional, but I found it necessary to make the font size of the paragraphs in our PDF bigger.

I created a style.css file in my working directory and wrote the code below.

p { font-size: 24px; }
li { font-size: 24px; }

That’s it. Here’s the PDF we generated with Python.

The code we wrote and the files used in this tutorial can be found on my GitHub.

Now to generate a more advanced PDF like the invoice below, we need to download an HTML template and create some placeholder. In this video tutorial, I show how to do it.


How to create PDF files in a few minutes using Python and HTML templates

Image via Shutterstock under license to Frank Andrade

PDF is probably one of the most common file types that we can find on our computers.

We use PDFs for our resumes, reports, invoices, you name it!

A common way to create a PDF file is by saving a Word file as .pdf, but we can also create a PDF file using Python.

The pros? If you plan to create multiple PDFs using the same template, you could do it with Python! But first, we’ll see how to create a PDF file using Python.

In this tutorial, we’ll first create a simple PDF file and then we’ll create a more advanced one that looks like the one below.

Image by author

There are different ways to create a PDF file with Python, but I found using HTML templates the easiest one.

With this option, we can create basic and advanced PDFs in a few minutes!

Install the libraries

To create a PDF file, we need to install the following libraries:

pip install pdfkit
pip install jinja2

Before we use thepdfkit library, we need to install wkhtmltopdf. The steps to install it is up to your operating system:

# macOS (you need to install brew first)
brew install homebrew/cask/wkhtmltopdf
# Ubuntu
sudo apt-get install wkhtmltopdf

In case you’re on Windows, you can download the installer here.

Now let’s create a simple PDF with Python.

Step 1: Create an HTML template with placeholders

First, we need to create an HTML document that we’ll use as a template later.

To create this HTML document, we’ll use a website called HTML Editor. On this website, we can type text in the visual editor on the left and generate its HTML code in the source editor on the right.

Here’s the text I introduced in the editor (this file can be found on my GitHub)

Note the placeholders I created using the {{}}. Later we’ll introduce values inside these curly braces with Python.

Here’s the HTML code that was generated by the HTML editor.

We need to copy this HTML code, create an HTML file in our working directory, and paste the code on it.

Step 2: Create a Python variable for each placeholder

First, we import jinja2 and pdfkit. I have a placeholder named {{today_date}}, so I also import datetime to get today’s date.

Then we create a Python variable for each placeholder in our HTML document and we create a dictionary that pairs the placeholders with the Python variables.

Note that I named each Python variable as the placeholders, but they could have different names.

Step 3: Create an environment for our template and export the PDF

Now it’s time to use jinja2 to create an environment for our template.

template_loader = jinja2.FileSystemLoader(‘./’)
template_env = jinja2.Environment(loader=template_loader)

Then we specify which template we’re using. In my case, I named the HTML file as basic-template.hmtl. After this, we render the dictionary we created in step 2.

template = template_env.get_template(‘basic-template.html’)
output_text = template.render(context)

Next, we add wkhtmltopdf to the pdfkit configuration. To do so, we need to specify the path where wkhtmltopdf was installed.

Here’s the command you need to run to get this path.

# macOS/Linux
>>> which wkhtmltopdf
'/usr/local/bin/wkhtmltopdf'
# Windows
>>> where wkhtmltopdf
'C:\Program Files\wkhtmltopdf'

Finally, we export the PDF file using the .from_string() method.

Note that I added a CSS parameter to the .from_string() method. This is optional, but I found it necessary to make the font size of the paragraphs in our PDF bigger.

I created a style.css file in my working directory and wrote the code below.

p { font-size: 24px; }
li { font-size: 24px; }

That’s it. Here’s the PDF we generated with Python.

The code we wrote and the files used in this tutorial can be found on my GitHub.

Now to generate a more advanced PDF like the invoice below, we need to download an HTML template and create some placeholder. In this video tutorial, I show how to do it.

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment