Python SDK for GeneratePDFs.com API
Project description
GeneratePDFs Python SDK
Python SDK for the GeneratePDFs.com API, your go-to place for HTML to PDF.
Upload your HTML files, along with any CSS files and images to generate a PDF. Alternatively provide a URL to generate a PDF from it's contents.
Installation
pip install generatepdfs-python-sdk
Get your API Token
Sign up for an account on GeneratePDFs.com and head to the API Tokens section and create a new token.
Usage
Basic Setup
from generatepdfs import GeneratePDFs
client = GeneratePDFs.connect('YOUR_API_TOKEN')
Generate PDF from HTML File
from generatepdfs import GeneratePDFs
# Simple HTML file
pdf = client.generate_from_html('/path/to/file.html')
# HTML file with CSS
pdf = client.generate_from_html(
'/path/to/file.html',
'/path/to/file.css'
)
# HTML file with CSS and images
pdf = client.generate_from_html(
'/path/to/file.html',
'/path/to/file.css',
[
{
'name': 'logo.png',
'path': '/path/to/logo.png',
'mime_type': 'image/png' # Optional, will be auto-detected
},
{
'name': 'photo.jpg',
'path': '/path/to/photo.jpg'
}
]
)
Generate PDF from URL
pdf = client.generate_from_url('https://example.com')
Get PDF by ID
# Retrieve a PDF by its ID
pdf = client.get_pdf(123)
Working with PDF Objects
The SDK returns Pdf objects that provide easy access to PDF information and downloading:
# Access PDF properties
pdf_id = pdf.get_id()
pdf_name = pdf.get_name()
status = pdf.get_status()
download_url = pdf.get_download_url()
created_at = pdf.get_created_at()
# Check if PDF is ready
if pdf.is_ready():
# Download PDF content as bytes
pdf_content = pdf.download()
# Or save directly to file
pdf.download_to_file('/path/to/save/output.pdf')
Refresh PDF data from the API (useful for checking status updates)
refreshed_pdf = pdf.refresh()
if refreshed_pdf.is_ready():
pdf_content = refreshed_pdf.download()
Client Methods
generate_from_html(html_path: str, css_path: Optional[str] = None, images: Optional[List[Dict[str, str]]] = None) -> Pdf- Generate a PDF from HTML file(s)generate_from_url(url: str) -> Pdf- Generate a PDF from a URLget_pdf(pdf_id: int) -> Pdf- Retrieve a PDF by its IDdownload_pdf(download_url: str) -> bytes- Download PDF binary content from a download URL
PDF Object Methods
get_id() -> int- Get the PDF IDget_name() -> str- Get the PDF filenameget_status() -> str- Get the current status (pending, processing, completed, failed)get_download_url() -> str- Get the download URLget_created_at() -> datetime- Get the creation dateis_ready() -> bool- Check if the PDF is ready for downloaddownload() -> bytes- Download and return PDF binary contentdownload_to_file(file_path: str) -> bool- Download and save PDF to a filerefresh() -> Pdf- Refresh PDF data from the API and return a new Pdf instance with updated information
Requirements
- Python 3.8 or higher
- requests library
Development Setup
To set up the development environment:
- Clone the repository:
git clone https://github.com/GeneratePDFs/python-sdk.git
cd python-sdk
- Create a virtual environment (recommended):
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install development dependencies:
pip install -r requirements-dev.txt
Or install the package in development mode:
pip install -e ".[dev]"
Testing
To run the test suite, execute:
pytest
To run tests with coverage:
pytest --cov=generatepdfs --cov-report=html
To run tests in verbose mode:
pytest -v
Contributing
Contributions and suggestions are welcome and will be fully credited.
We accept contributions via Pull Requests on GitHub.
Pull Requests
- Follow PEP 8 - Use a code formatter like
blackorautopep8to ensure code style consistency - Add tests! - Your patch won't be accepted if it doesn't have tests.
- Document any change in behaviour - Make sure the README / CHANGELOG and any other relevant documentation are kept up-to-date.
- Consider our release cycle - We try to follow semver. Randomly breaking public APIs is not an option.
- Create topic branches - Don't ask us to pull from your master branch.
- One pull request per feature - If you want to do more than one thing, send multiple pull requests.
- Send coherent history - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
Changelog
See CHANGELOG.md for a history of changes.
License
This project is licensed under the MIT License. See the LICENSE file for details.
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 generatepdfs_python_sdk-1.0.0.tar.gz.
File metadata
- Download URL: generatepdfs_python_sdk-1.0.0.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ff90dddc86a7db1da08e25024efd585d7179dfc8339109ccb9b70606e7115233
|
|
| MD5 |
4978a2b9792f797cd9b6a5a8bfbeb3d0
|
|
| BLAKE2b-256 |
e1d40702060d611b04c9de386053dd572da333fe2b968d4cfc5e9ad1187059cb
|
File details
Details for the file generatepdfs_python_sdk-1.0.0-py3-none-any.whl.
File metadata
- Download URL: generatepdfs_python_sdk-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bd189b6f6cb7b40ef33c526566cff50e14e462f276ddea6f79bf346740a983d
|
|
| MD5 |
8f80342becf2b2b85aca68d0138647d5
|
|
| BLAKE2b-256 |
18e4b4ef2527700a35efc3df805669153c13269c3ac45504350f188b56dc11f6
|