Adobe PDF Services SDK for Python

4.2.0 · active · verified Fri Apr 17

Adobe PDF Services SDK for Python provides client libraries to interact with Adobe Document Cloud PDF Services APIs. It enables developers to programmatically perform operations like PDF creation, export, compression, OCR, document generation, and more. The current version is 4.2.0. Releases are typically infrequent, corresponding to updates in the underlying Adobe API services.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the PDF Services client, authenticate using file-based credentials, and convert a simple HTML string to a PDF document. It highlights the use of `AdobeAuthCredentials` and `PdfServicesConfig` for setup, and `FileRef` for handling input/output files.

import os
from pdfservices_sdk.pdf_services_client import PdfServiceClient
from pdfservices_sdk.pdf_services_config import PdfServicesConfig
from pdfservices_sdk.auth.credentials import AdobeAuthCredentials
from pdfservices_sdk.io.file_ref import FileRef
from pdfservices_sdk.jobs.createpdf.create_pdf_from_html_job import CreatePdfFromHtmlJob

# Ensure you have your credentials.json file path set as an environment variable
# Or provide the path directly: credentials_path = "./pdfservices-api-credentials.json"
credentials_path = os.environ.get("PDF_SERVICES_SDK_CLIENT_CREDENTIALS_PATH", "./pdfservices-api-credentials.json")

# Create a dummy HTML file for conversion
dummy_html_content = "<h1>Hello from Adobe PDF Services!</h1><p>This is a test.</p>"
with open("input.html", "w") as f:
    f.write(dummy_html_content)

try:
    # 1. Build AdobeAuthCredentials using a file-based builder.
    #    Ensure credentials.json has 'client_id', 'client_secret', and 'organization_id'.
    credentials = AdobeAuthCredentials.file_based_credentials_builder()\
        .with_credentials_path(credentials_path).build()

    # 2. Build PdfServicesConfig with the created credentials.
    pdf_services_config = PdfServicesConfig.builder().with_credentials(credentials).build()

    # 3. Create a PdfServiceClient.
    pdf_service_client = PdfServiceClient.builder().with_pdf_services_config(pdf_services_config).build()

    # 4. Create an input FileRef from the HTML file.
    input_file_ref = FileRef.from_local_file("input.html")

    # 5. Create a CreatePdfFromHtmlJob instance.
    create_pdf_from_html_job = CreatePdfFromHtmlJob(input_file_ref)

    # 6. Execute the job and get the output FileRef.
    result_file_ref = pdf_service_client.execute(create_pdf_from_html_job)

    # 7. Save the result to a local file.
    output_path = "output.pdf"
    result_file_ref.save_as_file(output_path)
    print(f"Successfully created PDF: {output_path}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up dummy files
    if os.path.exists("input.html"):
        os.remove("input.html")
    # if os.path.exists("output.pdf"):
    #     os.remove("output.pdf") # Uncomment to remove generated PDF after successful run

view raw JSON →