Adobe PDF Services SDK for Python
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
-
AttributeError: module 'pdfservices_sdk.sdk_config' has no attribute 'SDKConfig'
cause Attempting to use the deprecated `SDKConfig` class from a pre-v4.x SDK version with a v4.x or later installation.fixReplace `SDKConfig` with `PdfServicesConfig`. Example: `from pdfservices_sdk.pdf_services_config import PdfServicesConfig`. -
AttributeError: type object 'ServicePrincipalCredentials' has no attribute 'file_based_credentials_builder'
cause Attempting to use the old file-based credentials builder method from `ServicePrincipalCredentials` (deprecated in v4.x) for file-based authentication.fixUse `AdobeAuthCredentials.file_based_credentials_builder()` instead. Example: `from pdfservices_sdk.auth.credentials import AdobeAuthCredentials; credentials = AdobeAuthCredentials.file_based_credentials_builder().with_credentials_path(...).build()`. -
FileNotFoundError: [Errno 2] No such file or directory: 'pdfservices-api-credentials.json'
cause The SDK cannot find your `credentials.json` file at the specified path, or the path is incorrect.fixVerify the path to your `credentials.json` file. Ensure it exists and has read permissions. Consider setting it via an environment variable `PDF_SERVICES_SDK_CLIENT_CREDENTIALS_PATH` for consistent access. -
pdfservices_sdk.exceptions.ServiceApiException: Error occurred while executing the API.
cause A general error from the Adobe PDF Services API. This often indicates issues with authentication (invalid credentials), malformed input files, or exceeding service limits.fixCheck your `credentials.json` for correctness. Examine the input file for any corruption or unsupported formats. Review the full exception details for more specific error codes or messages from the API. Implement robust retry mechanisms for transient errors.
Warnings
- breaking Major API changes in version 4.0.0 related to SDK configuration. The `SDKConfig` class has been replaced by `PdfServicesConfig`.
- breaking File-based authentication for `ServicePrincipalCredentials` was refactored in v4.x. You must now use `AdobeAuthCredentials.file_based_credentials_builder()` instead of `ServicePrincipalCredentials.file_based_credentials_builder()`.
- gotcha All API calls require valid `credentials.json` with `client_id`, `client_secret`, and `organization_id`.
- gotcha Input and output files are handled using `FileRef` objects. For local files, ensure the paths are correct and the SDK has read/write permissions.
Install
-
pip install pdfservices-sdk
Imports
- PdfServiceClient
from pdfservices_sdk.pdf_services_client import PdfServiceClient
- PdfServicesConfig
from pdfservices_sdk.sdk_config import SDKConfig
from pdfservices_sdk.pdf_services_config import PdfServicesConfig
- AdobeAuthCredentials
from pdfservices_sdk.auth.credentials import ServicePrincipalCredentials
from pdfservices_sdk.auth.credentials import AdobeAuthCredentials
- FileRef
from pdfservices_sdk.io.file_ref import FileRef
- CreatePdfFromHtmlJob
from pdfservices_sdk.jobs.createpdf.create_pdf_from_html_job import CreatePdfFromHtmlJob
Quickstart
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