How to Convert HTML to PDF in Python?
This example is focused on python create pdf from html. This example will help you how to convert html to pdf in python. I am going to show you about how to convert html file to pdf in python. Here you will learn html to pdf in python example. Let's get started with python convert html to pdf.
In this example, I will show you how to create pdf file from html file in python, we will use pdfkit and wkhtmltopdf to generate pdf file from html 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:
Step 3: Create HTML File
Here, we will create sample.html file with some dummy date. let's copy below code and create file in root directory:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello!</title> </head> <body> <h1>Welcome to ItSolutionStuff.com!</h1> <p>This is a sample HTML file.</p> </body> </html>
Step 4: 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_file('sample.html', '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_file('sample.html', 'sample.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:
I hope it can help you...
We are Recommending you
- How to Create a PDF File in Python?
- Python Pandas Read Excel File Multiple Sheets Example
- Python Write Excel File with Multiple Sheets Example
- Python Xlsxwriter Create Excel File Example
- How to Convert JSON to Array in Laravel?
- How to Modify JSON File in Python?
- Python Read and Write a JSON File Example
- How to Get Column Names from CSV File in Python?
- Python Read CSV File Line by Line Example
- Python Read CSV File Without Header Example
- How to Read Text File Line by Line in Python?
- How to Append Text or Lines to a Text File in Python?
- Python Generate Text File from List Example