Skip to main content

Python client for pdfapi.dev - HTML to PDF conversion service

Project description

Python Client for pdfapi.dev

pdfapi.dev logo

A Python client library for pdfapi.dev - HTML to PDF conversion service that supports:

  • CSS styling
  • Custom headers and footers
  • Images and other assets
  • Multiple page formats
  • Configurable margins and scaling

Installation

pip install pdfapi-client

Quick Start

from pdfapi_client import PDFApiClient, ConversionConfig, PageFormat

# Initialize the client
client = PDFApiClient(api_key="your-api-key")

# Convert HTML to PDF
pdf_bytes = client.convert_html(
    html="<h1>Hello World</h1>",
    config=ConversionConfig(format=PageFormat.A4)
)

# Save to file
with open("output.pdf", "wb") as f:
    f.write(pdf_bytes)

Examples

HTML with CSS

html = """
<html>
    <head>
        <link rel="stylesheet" href="style.css">
    </head>
    <body>
        <h1>Styled Document</h1>
        <p>This is a styled paragraph.</p>
    </body>
</html>
"""

css = "h1 { color: navy; } p { color: gray; }"

pdf_bytes = client.convert_html(
    html=html,
    assets={"style.css": css},
    config=ConversionConfig(format=PageFormat.A4)
)

With Header and Footer

from pathlib import Path

config = ConversionConfig(
    format=PageFormat.A4,
    margin=Margin(top=40, bottom=40, left=20, right=20),
    header_file="<div style='text-align: center'>My Document</div>",
    footer_file="<div style='text-align: right'>Page {page} of {pages}</div>"
)

pdf_bytes = client.convert_html(
    html=Path("document.html"),  # Can also load from file
    config=config
)

With Images

html = """
<html>
    <body>
        <h1>Document with Image</h1>
        <img src="image.png" alt="My Image">
    </body>
</html>
"""

pdf_bytes = client.convert_html(
    html=html,
    assets={"image.png": Path("path/to/image.png")},
    config=ConversionConfig(format=PageFormat.A4)
)

Configuration Options

Page Formats

Available page formats:

  • PageFormat.A4 (default)
  • PageFormat.A3
  • PageFormat.LETTER
  • PageFormat.LEGAL
  • And more...

Margins

Margins can be specified in pixels:

config = ConversionConfig(
    margin=Margin(
        top=20,
        bottom=20,
        left=20,
        right=20
    )
)

Other Options

  • scale: Scale factor for the output (default: 1.0)
  • landscape: Whether to use landscape orientation (default: False)

Input Types

The library accepts various input types:

  • Strings (for direct content)
  • Path objects (for file paths)
  • File-like objects (for open files)
  • Bytes (for binary content)

This applies to HTML content, CSS, images, and header/footer files.

Error Handling

from pdfapi_client import PDFApiClient, ConversionError

try:
    pdf_bytes = client.convert_html(html="<h1>Test</h1>")
except ConversionError as e:
    print(f"Conversion failed: {e}")

License

Copyright 2024 pdfapi.dev

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the 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

pdfapi_client-0.1.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

pdfapi_client-0.1.3-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pdfapi_client-0.1.3.tar.gz.

File metadata

  • Download URL: pdfapi_client-0.1.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pdfapi_client-0.1.3.tar.gz
Algorithm Hash digest
SHA256 270536925ee823ee905d9819cef142ff126c5220abc5fcc196a703b47fdfc7bf
MD5 704f3f8830fd88c2ae1b4d7b39e14195
BLAKE2b-256 befb8967b4806972d3ea206705320b7ecea4e9aacbb3d73e2d7253c6ec160c3e

See more details on using hashes here.

File details

Details for the file pdfapi_client-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: pdfapi_client-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for pdfapi_client-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0e1d6f5d8e0e5ee86f2746ab5d41e3bdc5afcdec52dc1ad69e11e7bfe9f07243
MD5 a2d25e09f71ea748edf8dc1d53740657
BLAKE2b-256 e6a5745a5ff8138bbe55dc183ab66db7f6f0be46b2e908d7c45147834f0882f4

See more details on using hashes here.

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