Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 PDFKit Example to Convert HTML5 Website URL to PDF Document in Command Line

Posted on December 21, 2022

 

 

Welcome folks today in this blog post we will be converting html5 website url to pdf document using pdfkit library in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the below library using the pip command as shown below

 

 

pip install pdfkit

 

 

After that you need to make an app.py file and copy paste the following code

 

 

app.py

 

 

Python
1
2
3
4
5
6
7
8
9
10
11
import pdfkit
 
# Create a PDF document object
options = {
    'page-size': 'A4',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
}
pdfkit.from_url('http://micropyramid.com', 'micro.pdf', options=options)

 

 

As you can see we are importing the pdfkit library at the very top and then we are declaring an object which contains the properties of the pdf document that we will be generating such as the page size and different margins from different directions. And after that we need to generate the pdf document from the url. For this we are using the from_url()method and inside this we are passing the url of the html5 website that we want to export to pdf document and then we are passing the output file name and then we are passing the options as the third argument

 

Now if you try to run this python script in the command line you will see it will generate the pdf file as shown below

 

 

 

Recent Posts

  • Android Java Project to Merge Multiple PDF Documents From Gallery Using iTextPDF Library
  • Android Java Project to Detect System Hardware & System CPU Info & Display inside TextView Widget
  • Android Java Project to Integrate Google OAuth2 Login & Logout System & Save User Info in SharedPreferences
  • Android Java Project to Export Raw Text to PDF Document Using iTextPDF Library
  • Android Java Project to Export Images From Gallery to PDF Document Using iTextPDF Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme