ItSolutionStuff.com

Python Create PDF File from Text Example

By Hardik Savani • October 30, 2023
Python

Hi Friends,

In this tutorial, you will learn python create pdf file from text. you can see how to create pdf from text in python. This article goes in detailed on pdf from text in python. let us discuss about python create pdf from text. Alright, let us dive into the details.

In this example, I will show you how to create pdf file from plain text file in python, we will use pdfkit and wkhtmltopdf to generate pdf file from text in python. So, without further ado, let's follow simple steps with python3 (Python 3) version.

Step 1: Install PDFKit Library

In this step, we need to install pdfkit library by the following the command:

pip3 install pdfkit

Step 2: Install wkhtmltopdf Software

In this step, we need to install wkhtmltopdf software to your system, so let's use following way for your system.

For Ubuntu User:

sudo apt-get update
sudo apt-get install wkhtmltopdf

For Windows User:

Download wkhtmltopdf Exe File

Step 3: Create Python File

Here, we will create main.py python file and write code to generate pdf file. i will give you two example code one for ubuntu user and another for windows user. so let's see the bellow code:

main.py (For Ubuntu Users)

import pdfkit
  
# Generate PDF File Code
pdfkit.from_string('This is from ItSolutionStuff.com','out.pdf')

main.py (For Windows Users)

import pdfkit
  
# Define Path of wkhtmltopdf.exe
pathToWkhtmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
  
# Point pdfkit configuration to wkhtmltopdf.exe
config = pdfkit.configuration(wkhtmltopdf=pathToWkhtmltopdf)
  
# Convert HTML file to PDF File
pdfkit.from_string('This is from ItSolutionStuff.com', 'out.pdf', configuration=config)

Run Python App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

python3 main.py

Output:

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Convert HTML to PDF in Python?

Read Now →

How to Generate PDF using PDFKit with Python?

Read Now →

How to Create a PDF File in Python?

Read Now →

How to Convert Dictionary to JSON in Python?

Read Now →

How to Convert JSON to Array in Laravel?

Read Now →

How to Modify JSON File in Python?

Read Now →

Python Create JSON File from Dict Example

Read Now →

Python Read CSV File Line by Line Example

Read Now →

Python Read CSV File Without Header Example

Read Now →

Python Read Text File into List Example

Read Now →

How to Read Text File Line by Line in Python?

Read Now →

How to Read a Text File in Python?

Read Now →

Python Create Text File If Not Exists Example

Read Now →