Skip to main content

PDFGate's official Python SDK

Project description

PDFGate's official Python SDK

PDFGate lets you generate, process, and secure PDFs via a simple API:

  • HTML or URL to PDF
  • Fillable forms
  • Flatten, compress, watermark, protect PDFs
  • Extract PDF form data

📘 Documentation: https://pdfgate.com/documentation
🔑 Dashboard & API keys: https://dashboard.pdfgate.com

pdfgate-sdk-python

PyPI - Version PyPI - Python Version

Table of Contents

Installation

pip install pdfgate

Quick start

import os

from pdfgate import PDFGate, GeneratePDFParams


client = PDFGate(api_key=os.getenv("PDFGATE_API_KEY"))
params = GeneratePDFParams(url="https://example.com")
pdf = client.generate_pdf(params)

with open("output.pdf", "wb") as f:
  f.write(pdf)

Sync & Async

There are sync and async versions of all methods, the only difference is that the method name has an async suffix:

document_response = client.get_document(GetDocumentParams(document_id=document_id))

# VS

document_response = await client.get_document_async(GetDocumentParams(document_id=document_id))

Other than that, nothing changes and the interfaces are the same.

Responses

The two response types for most endpoints are either the raw bytes of the PDF file or the documents metadata that you can use to query or download later:

  • file: this is represented by bytes and it can be saved directly into a file.
  • PDFGateDocument: this is a TypedDict that holds all the metadata of the file including its id, and a file_url to download it if it was requested by specifying pre_signed_url_expires_in in the request.

Examples

Generate PDF

params = GeneratePDFParams(html="<h1>Hello from PDFGate!</h1>")
pdf = client.generate_pdf(params)

with open("output.pdf", "wb") as f:
  f.write(pdf)

Get document metadata

document_response = client.get_document(GetDocumentParams(document_id=document_id))

assert document_response = {
    "id" : "6642381c5c61"
    "status" : DocumentStatus.COMPLETED
    "type" : DocumentType.FROM_HTML,
    "file_url" : "https://api.pdfgate.com/file/open/:preSignedUrlToken"
    "size" : 1620006
    "createdAt" : datetime(2024,02,13, 15, 56, 12, 607)"
}

Download a stored PDF file

file_content = client.get_file(GetFileParams(document_id=document_id))

with open("output.pdf", "wb") as f:
  f.write(pdf)

Flatten a PDF (make form-fields non-editable)

flatten_pdf_params = FlattenPDFDocumentParams(
    document_id=document_id, json_response=True
)
flattened_document = client.flatten_pdf(flatten_pdf_params)

Compress a PDF

compress_pdf_params = CompressPDFByDocumentIdParams(
    document_id=document_id, json_response=True
)
response = client.compress_pdf(compress_pdf_params)

Watermark a PDF

watermark_pdf_params = WatermarkPDFByFileParams(
    file=FileParam(name="input.pdf", data=pdf_file),
    type=WatermarkType.IMAGE,
    watermark=FileParam(name="watermark.jpg", data=jpg_file),
)
watermarked_pdf = cast(PDFGateDocument, client.watermark_pdf(watermark_pdf_params))

Protect (encrypt) a PDF

protect_pdf_params = ProtectPDFByDocumentIdParams(
    document_id=document_id,
    user_password=str(uuid.uuid4()),
    owner_password=str(uuid.uuid4()),
    json_response=True,
)
response = client.protect_pdf(protect_pdf_params)

Extract PDF form fields values

html_form = """
    <form>
        <input type='text' name='first_name' value='John'/>
        <input type='text' name='last_name' value='Doe'/>
    </form>
    """
generate_pdf_params = GeneratePDFParams(
    html=html_form, enable_form_fields=True, json_response=True
)
document_response = cast(PDFGateDocument, client.generate_pdf(generate_pdf_params))
document_id = cast(str, document_response.get("id"))

extract_form_params = ExtractPDFFormDataByDocumentIdParams(document_id=document_id)
response = cast(dict[str, Any], client.extract_pdf_form_data(extract_form_params))

Development

Before doing anything, install pre-commit by running:

hatch run dev:pre-commit install

This will run several checks every time you try to git commit including:

  • linting and formatting with Ruff
  • type checking with mypy

If you are on VS Code, it's recommended to install the Ruff extension so you'll get formatting on the fly.

Hatch is used as a build system and for dependency management, so all main actions are configured to be run with Hatch.

Tests

Unit tests:

hatch run test:test

Acceptance tests hit the PDFGate API so they are slower, and require an API key that is expected to be set as an env var named PDFGATE_API_KEY. You can set it on your Bash/zsh/fish profile or inline as in:

PDFGATE_API_KEY="test_123" hatch run test:test_acceptance

Manually run Ruff

Linting:

hatch run dev:lint

Formatting:

hatch run dev:ruff format .

Type checking

Manually mypy for type checking:

hatch run dev:check

Docs

Docs are build using MkDocs, they live in the docs/ folder, and in the code. If you make any changes, and would like to see them live before publishing them, spin up a server locally with:

hatch run docs:serve

Changes to docs/** and mkdocs.yml trigger a new deployment of the docs site. If you change the code's documentation and want to manually update the docs site you can do it from the Actions tab of the repo or by running:

hatch run docs:mkdocs gh-deploy

Support

📧 Email: support@pdfgate.com
📘 Docs: https://pdfgate.com/documentation

License

pdfgate-sdk-python is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pdfgate-0.0.2.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pdfgate-0.0.2-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file pdfgate-0.0.2.tar.gz.

File metadata

  • Download URL: pdfgate-0.0.2.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pdfgate-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e674c48f9cace932ab193e9cf6ee64b53705a2dfd0ecceb89b41ec276d31e555
MD5 0d9e69466b753608028d8ba64dcbeba0
BLAKE2b-256 afdd89dd2235b2b34b8bfc88ca5656b96bb9e6449ab3023e2ab81c6b7899e426

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdfgate-0.0.2.tar.gz:

Publisher: publish-to-pypi.yml on pdfgate/pdfgate-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pdfgate-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pdfgate-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pdfgate-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c337236ec1be1dde8ce98c01320bc770ff25a8567bbd922c14cbf07fd4571294
MD5 c4870b3bbeb30262db8b4261c373f4d5
BLAKE2b-256 bcc26fbfe27cd04ef442dd8bcfc927af5d608d9defdc26b0a0fc764e44dfb848

See more details on using hashes here.

Provenance

The following attestation bundles were made for pdfgate-0.0.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on pdfgate/pdfgate-sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page