Tool for converting HTML files to PDF using Sphinx and Playwright.
Project description
User Guide for Sphinx-Each-PDF
Introduction
Sphinx-Each-PDF is a Sphinx extension that generates a separate PDF file for each page in a documentation project. It uses Playwright for rendering and integrates seamlessly with Sphinx. This is particularly useful for creating static websites where each page can be downloaded as an individual PDF.
Installation
-
Ensure you have Python 3.8 or newer installed.
-
Install the package from PyPI:
pip install sphinx-each-pdf
-
Install Chromium for Playwright (or any other supported engine, like Firefox or WebKit, based on your preference):
playwright install chromium
You may replace
chromiumwithfirefoxorwebkitif desired. -
(Recommended) Install system dependencies for Chromium to ensure smooth operation:
playwright install-deps chromium
Package Structure
-
convert.py:
- Contains the
convert_html_to_pdffunction, which converts individual HTML pages to PDFs. - Utilizes Playwright for rendering HTML with CSS support.
- Removes unnecessary links around images for cleaner output.
- Contains the
-
each_pdf.py:
- Defines the
CustomBuilderclass, extending Sphinx functionality. - Scans the Sphinx build directory for HTML files and generates a separate PDF for each page.
- Defines the
-
init.py:
- Integrates the extension with Sphinx by adding the custom Sphinx-Each-PDF builder.
- Adds PDF download links for individual pages in the generated HTML.
Adding a PDF Download Link
To automatically include a download link to the PDF version of each page in the generated documentation, follow these steps:
-
Enable the
pdf_linkVariable:- The extension automatically sets a Sphinx variable
pdf_linkin the context of each page. This variable contains the relative path to the PDF file for the current page.
- The extension automatically sets a Sphinx variable
-
Customize the Page Template:
- Create or edit the
_templates/layout.htmlfile in your Sphinx project (or create the_templatesdirectory if it does not exist). - Add the following block of code where you want the PDF download link to appear (e.g., in the footer or header):
{% if pdf_link %} <a href="{{ pdf_link }}" class="pdf-download-link">Download PDF</a> {% endif %} - This will insert a link to the corresponding PDF file for each page if it exists.
- Create or edit the
-
Configure Sphinx to Use Custom Templates:
- In your
conf.pyfile, add the_templatesdirectory to thetemplates_pathconfiguration:templates_path = ['_templates']
- In your
-
Apply Custom Styling (Optional):
- Add CSS rules to style the link (e.g., in your custom CSS file or the default
sphinx-each-pdf.css):.pdf-download-link { display: inline-block; margin-top: 10px; padding: 5px 10px; background-color: #007BFF; color: white; text-decoration: none; border-radius: 5px; } .pdf-download-link:hover { background-color: #0056b3; }
- Add CSS rules to style the link (e.g., in your custom CSS file or the default
Usage
Connecting the Extension
You can enable the Sphinx-Each-PDF extension in Sphinx in one of two ways:
-
Automatic Builder Mode:
- Add the following line to your
conf.pyfile:extensions.append('sphinx_each_pdf')
- Add the following line to your
-
Manual Extension Declaration:
- Add
'sphinx_each_pdf'to your list of Sphinx extensions:extensions = [ 'sphinx.ext.autodoc', 'sphinx_each_pdf', # Add here ]
- Add
Running the Build
After adding the extension, run the following Sphinx build command:
sphinx-build -b each-pdf source_dir build_dir
Here:
source_diris the directory containing your Sphinx source files (e.g.,docs).build_diris the directory where the generated files will be placed (e.g.,_build).
After the build completes, you will find a separate PDF file for each HTML page in the build_dir.
Custom Styles
By default, the extension applies styles from the sphinx-each-pdf.css file in your Sphinx project directory.
You can replace this file or specify a custom CSS path during the conversion process. The default CSS file path is:
<source_dir>/sphinx-each-pdf.css
Manual HTML to PDF Conversion
If needed, you can use convert.py as a standalone script to convert an individual HTML file to a PDF:
python convert.py <html_path> <pdf_path> [css_path]
<html_path>: Path to the source HTML file.<pdf_path>: Path to save the resulting PDF.[css_path]: (Optional) Path to a CSS file for custom styling.
Notes
-
The tool uses Playwright, which supports Chromium, Firefox, and WebKit. By default,
chromiumis recommended. However, you can switch to another engine (e.g., Firefox or WebKit) by replacingchromiumin the installation command:playwright install <engine>
-
Install system dependencies for your chosen engine to avoid runtime errors:
playwright install-deps <engine>
Replace
<engine>withchromium,firefox, orwebkit. -
The each_pdf.py script uses multithreading to process multiple files efficiently.
License
This tool is distributed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sphinx_each_pdf-1.0.0.tar.gz.
File metadata
- Download URL: sphinx_each_pdf-1.0.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1449ebdddf28ab95df95dd1f106b0b6115493efb5b4a538b85261a60bdd835f3
|
|
| MD5 |
a8a91030df46c80bd0e6bfd2270b6867
|
|
| BLAKE2b-256 |
57071d0185873ba41e96b5e18d05e215ba4444476c67752242710e85bef76806
|
File details
Details for the file sphinx_each_pdf-1.0.0-py3-none-any.whl.
File metadata
- Download URL: sphinx_each_pdf-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a610f11b1495a9d3c1caf976cd2949b161d85f09dfbf40ceef8c7338e9c13b28
|
|
| MD5 |
ba226e08fef8d417f7ded75337f130ca
|
|
| BLAKE2b-256 |
362b67b6a9fcb8033e95db2196d649059b492247ccf3985822cc37490eea79b6
|