Sometimes, you come across a webpage that is so good you have to save it for later :) Especially the docs! That's exactly why I created this script (wrapper for pdfkit :) ).
WebPDF is a Python script that provides a simple interface to convert web pages or HTML files into PDF documents using the wkhtmltopdf library. This script allows users to easily generate PDFs from URLs or local HTML files with various configuration options.
- Convert web pages to PDF using URLs.
- Convert local HTML files to PDF.
- Automatically checks for required dependencies (pdfkit and wkhtmltopdf).
This script requires the following:
- pdfkit: A Python wrapper for wkhtmltopdf that simplifies the process of generating PDFs.
- wkhtmltopdf: A command-line tool to render HTML into PDF and other formats.
sudo apt install python3-pdfkit
Additionally, you need to have wkhtmltopdf installed on your system. You can install it using the following commands based on your operating system:
sudo apt install wkhtmltopdf
brew install wkhtmltopdf
Download the installer from the wkhtmltopdf website and follow the installation instructions.
git clone https://github.com/BarriosXJavier/WebPDF.git
cd WebPDF
On Linux or macOS, you'll need to make the script executable by running:
chmod u+x webpdf.py
Ensure you have installed pdfkit and wkhtmltopdf as described in the Dependencies section.
To make the webpdf.py script available globally so that you can run it from any directory, you can add it to your system's PATH.
Copy the script to a directory that is already in your PATH or create a symbolic link:
sudo cp webpdf.py /usr/local/bin/webpdf
Alternatively, create a symlink:
ln -s $(pwd)/webpdf.py /usr/local/bin/webpdf
Ensure /usr/local/bin is in your PATH. You can check by running:
echo $PATH
If it's not in your PATH, you can add it by adding the following line to your ~/.bashrc
(for Bash users) or ~/.zshrc
(for Zsh users):
export PATH="/usr/local/bin:$PATH"
After adding, run:
source ~/.bashrc # For Bash users
# or
source ~/.zshrc # For Zsh users
Copy the script to a directory included in the system PATH, or modify the PATH to include the script's directory. Here’s how to add it to PATH:
- Go to Control Panel > System and Security > System > Advanced System Settings.
- Click on Environment Variables.
- In the System Variables section, find the Path variable, select it, and click Edit.
- Add the full path to the folder containing webpdf.py (for example, C:\path\to\webpdf).
Once added, you should be able to run the script by typing webpdf.py
in any terminal or command prompt window.
To use the script, run the following command in your terminal:
webpdf [options] <url_or_html_file>
This assumes you've added the script to your PATH (as described in the section above).
If not, you can still run it by calling:
python3 /path/to/webpdf.py [options] <url_or_html_file>
webpdf https://www.example.com
webpdf local_file.html