Skip to content

Commit

Permalink
Merge pull request #204 from AdobeDocs/develop
Browse files Browse the repository at this point in the history
Python SDK v4.0.0 updates
  • Loading branch information
jasnoors authored May 22, 2024
2 parents f7b7804 + 68285e8 commit 3b2e32e
Show file tree
Hide file tree
Showing 30 changed files with 1,784 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Obtain free credentials

## Step 2

Download ready to run samples for Node.js, .Net, Java and Python
Download ready to run samples for Node.js, Java, .Net, and Python

- [Node.js](https://github.com/adobe/pdfservices-node-sdk-samples)
- [Java](https://github.com/adobe/pdfservices-java-sdk-samples)
Expand Down
2 changes: 1 addition & 1 deletion src/pages/apis/doc-generation/dg-start-modifying-pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Start with the Free Tier and get 500 free Document Transactions per month.

## Step 1

Start a free tier and download code samples for Node.js, Java, and .Net
Start a free tier and download code samples for Node.js, Java, .Net, and Python

- [Get started](https://acrobatservices.adobe.com/dc-integration-creation-app-cdn/main.html?api=document-generation-api)

Expand Down
3 changes: 2 additions & 1 deletion src/pages/apis/electronic-seal-api/seal-api-stepper.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ Obtain free credentials

## Step 2

Download ready to run samples for Node.js, .Net, Java
Download ready to run samples for Node.js, Java, .Net, and Python

- [Node.js](https://github.com/adobe/pdfservices-node-sdk-samples)
- [Java](https://github.com/adobe/pdfservices-java-sdk-samples)
- [.Net](https://github.com/adobe/PDFServices.NET.SDK.Samples)
- [Python](https://github.com/adobe/pdfservices-python-sdk-samples)

<TextBlock slots="heading, text, buttons" width="33%" theme="lightest" className='align-left horizontal-align link extract-stepper-api-reference Explore-other-Adobe-Document-Services-APIs' headerElementType="h2" />

Expand Down
2 changes: 1 addition & 1 deletion src/pages/apis/pdf-extract/extract-stepper.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Obtain free credentials

## Step 2

Download ready to run samples for Node.js, .Net, Java and Python
Download ready to run samples for Node.js, Java, .Net, and Python

- [Node.js](https://adobe.com/go/dcExtract_node_sdk)
- [Java](https://adobe.com/go/dcExtract_java_sdk)
Expand Down
68 changes: 42 additions & 26 deletions src/pages/apis/pdf-services/accessibility-auto-tag-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,39 +234,55 @@ public class AutotagPDF {
# Run the sample:
# python src/autotagpdf/autotag_pdf.py

logging.basicConfig(level=os.environ.get('LOGLEVEL', 'INFO'))
# Initialize the logger
logging.basicConfig(level=logging.INFO)

try:
# get base path.
base_path = str(Path(__file__).parents[2])

# Initial setup, create credentials instance.
credentials = Credentials.service_principal_credentials_builder()
.with_client_id('PDF_SERVICES_CLIENT_ID')
.with_client_secret('PDF_SERVICES_CLIENT_SECRET')
.build()
class AutoTagPDF:
def __init__(self):
try:
file = open("autotagPDFInput.pdf", "rb")
input_stream = file.read()
file.close()

# Create an ExecutionContext using credentials and create a new operation instance.
execution_context = ExecutionContext.create(credentials)
autotag_pdf_operation = AutotagPDFOperation.create_new()
# Initial setup, create credentials instance
credentials = ServicePrincipalCredentials(
client_id=os.getenv("PDF_SERVICES_CLIENT_ID"),
client_secret=os.getenv("PDF_SERVICES_CLIENT_SECRET"),
)

# Set operation input from a source file.
input_file_path = 'autotagPdfInput.pdf'
source = FileRef.create_from_local_file(base_path + '/resources/' + input_file_path)
autotag_pdf_operation.set_input(source)
# Creates a PDF Services instance
pdf_services = PDFServices(credentials=credentials)

# Execute the operation.
autotag_pdf_output: AutotagPDFOutput = autotag_pdf_operation.execute(execution_context)
# Creates an asset(s) from source file(s) and upload
input_asset = pdf_services.upload(
input_stream=input_stream, mime_type=PDFServicesMediaType.PDF
)

input_file_name = Path(input_file_path).stem
base_output_path = base_path + '/output/AutotagPDF/'
# Creates a new job instance
autotag_pdf_job = AutotagPDFJob(input_asset)

Path(base_output_path).mkdir(parents=True, exist_ok=True)
tagged_pdf_path = f'{base_output_path}{input_file_name}-tagged.pdf'
# Submit the job and gets the job result
location = pdf_services.submit(autotag_pdf_job)
pdf_services_response = pdf_services.get_job_result(
location, AutotagPDFResult
)

# Save the result to the specified location.
autotag_pdf_output.get_tagged_pdf().save_as(tagged_pdf_path)
# Get content from the resulting asset(s)
result_asset: CloudAsset = (
pdf_services_response.get_result().get_tagged_pdf()
)
stream_asset: StreamAsset = pdf_services.get_content(result_asset)

except (ServiceApiException, ServiceUsageException, SdkException) as e:
logging.exception(f'Exception encountered while executing operation: {e}')
# Creates an output stream and copy stream asset's content to it
output_file_path = "autoTagPDFOutput.pdf"
with open(output_file_path, "wb") as file:
file.write(stream_asset.get_input_stream())
except (ServiceApiException, ServiceUsageException, SdkException) as e:
logging.exception(f"Exception encountered while executing operation: {e}")
if __name__ == "__main__":
AutoTagPDF()
```
74 changes: 73 additions & 1 deletion src/pages/apis/pdf-services/combine-pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Combine two or more documents into a single PDF file

See our public [API Reference](https://developer.adobe.com/document-services/docs/apis/#tag/Combine-PDF) and quickly try our APIs using the Postman collections.

<CodeBlock slots="heading, code" repeat="4" languages="curl, js,.net,java" />
<CodeBlock slots="heading, code" repeat="5" languages="curl, js,.net,java,python" />

#### REST API

Expand Down Expand Up @@ -247,3 +247,75 @@ namespace CombinePDF
}
}
```
#### Python
```python
# Get the samples https://github.com/adobe/pdfservices-python-sdk-samples
# Run the sample:
# python src/combinepdf/combine_pdf.py

# Initialize the logger
logging.basicConfig(level=logging.INFO)


class CombinePDF:
def __init__(self):
try:
file = open("./combineFilesInput1.pdf", "rb")
input_stream_1 = file.read()
file.close()

file = open("./combineFilesInput2.pdf", "rb")
input_stream_2 = file.read()
file.close()

# Initial setup, create credentials instance
credentials = ServicePrincipalCredentials(
client_id=os.getenv("PDF_SERVICES_CLIENT_ID"),
client_secret=os.getenv("PDF_SERVICES_CLIENT_SECRET"),
)

# Creates a PDF Services instance
pdf_services = PDFServices(credentials=credentials)

# Creates an asset(s) from source file(s) and upload
stream_assets = [
StreamAsset(input_stream_1, PDFServicesMediaType.PDF),
StreamAsset(input_stream_2, PDFServicesMediaType.PDF),
]

assets = pdf_services.upload_assets(stream_assets)

# Create parameters for the job
combine_pdf_params = (CombinePDFParams().add_asset(assets[0])).add_asset(
assets[1]
)

# Creates a new job instance
combine_pdf_job = CombinePDFJob(combine_pdf_params=combine_pdf_params)

# Submit the job and gets the job result
location = pdf_services.submit(combine_pdf_job)
pdf_services_response = pdf_services.get_job_result(
location, CombinePDFResult
)

# Get content from the resulting asset(s)
result_asset: CombinePDFResult = (
pdf_services_response.get_result().get_asset()
)
stream_asset: StreamAsset = pdf_services.get_content(result_asset)

# Creates an output stream and copy stream asset's content to it
output_file_path = "output/CombinePDF.pdf"
with open(output_file_path, "wb") as file:
file.write(stream_asset.get_input_stream())
except (ServiceApiException, ServiceUsageException, SdkException) as e:
logging.exception(f"Exception encountered while executing operation: {e}")
if __name__ == "__main__":
CombinePDF()
```
60 changes: 59 additions & 1 deletion src/pages/apis/pdf-services/compress-pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Support for multiple compression levels to retain the quality of images and grap

See our public [API Reference](https://developer.adobe.com/document-services/docs/apis/#tag/Compress-PDF) and quickly try our APIs using the Postman collections

<CodeBlock slots="heading, code" repeat="4" languages="curl,JS,.NET, Java" />
<CodeBlock slots="heading, code" repeat="5" languages="curl,JS,.NET, Java,python" />

#### REST API

Expand Down Expand Up @@ -215,3 +215,61 @@ public class CompressPDF {
}
}
```
#### Python
```python
# Get the samples https://github.com/adobe/pdfservices-python-sdk-samples
# Run the sample:
# python src/compresspdf/compress_pdf.py

# Initialize the logger
logging.basicConfig(level=logging.INFO)


class CompressPDF:
def __init__(self):
try:
file = open("./compressPDFInput.pdf", "rb")
input_stream = file.read()
file.close()

# Initial setup, create credentials instance
credentials = ServicePrincipalCredentials(
client_id=os.getenv("PDF_SERVICES_CLIENT_ID"),
client_secret=os.getenv("PDF_SERVICES_CLIENT_SECRET"),
)

# Creates a PDF Services instance
pdf_services = PDFServices(credentials=credentials)

# Creates an asset(s) from source file(s) and upload
input_asset = pdf_services.upload(
input_stream=input_stream, mime_type=PDFServicesMediaType.PDF
)

# Creates a new job instance
compress_pdf_job = CompressPDFJob(input_asset=input_asset)

# Submit the job and gets the job result
location = pdf_services.submit(compress_pdf_job)
pdf_services_response = pdf_services.get_job_result(
location, CompressPDFResult
)

# Get content from the resulting asset(s)
result_asset: CloudAsset = pdf_services_response.get_result().get_asset()
stream_asset: StreamAsset = pdf_services.get_content(result_asset)

# Creates an output stream and copy stream asset's content to it
output_file_path = "output/CompressPDF.pdf"
with open(output_file_path, "wb") as file:
file.write(stream_asset.get_input_stream())
except (ServiceApiException, ServiceUsageException, SdkException) as e:
logging.exception(f"Exception encountered while executing operation: {e}")
if __name__ == "__main__":
CompressPDF()
```
71 changes: 70 additions & 1 deletion src/pages/apis/pdf-services/convert-pdf.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Support for PDF to DOC, PDF to DOCX, PDF to JPEG, PDF to PNG, PDF to PPTX, PDF t

See our public [API Reference](https://developer.adobe.com/document-services/docs/apis/#tag/Export-PDF) and quickly try our APIs using the Postman collections

<CodeBlock slots="heading, code" repeat="4" languages="curl,JS,.NET, Java" />
<CodeBlock slots="heading, code" repeat="5" languages="curl,JS,.NET, Java,python" />

#### REST API

Expand Down Expand Up @@ -233,3 +233,72 @@ public class ExportPDFToDOCX {
}
}
```
#### Python
```python
# Get the samples https://github.com/adobe/pdfservices-python-sdk-samples
# Run the sample:
# python python src/exportpdftoimages/export_pdf_to_jpeg.py

# Initialize the logger
logging.basicConfig(level=logging.INFO)

class ExportPDFToDOCX:
def __init__(self):
try:
file = open("./exportPDFToImageInput.pdf", "rb")
input_stream = file.read()
file.close()

# Initial setup, create credentials instance
credentials = ServicePrincipalCredentials(
client_id=os.getenv("PDF_SERVICES_CLIENT_ID"),
client_secret=os.getenv("PDF_SERVICES_CLIENT_SECRET"),
)

# Creates a PDF Services instance
pdf_services = PDFServices(credentials=credentials)

# Creates an asset(s) from source file(s) and upload
input_asset = pdf_services.upload(
input_stream=input_stream, mime_type=PDFServicesMediaType.PDF
)

# Create parameters for the job
export_pdf_to_images_params = ExportPDFtoImagesParams(
export_pdf_to_images_target_format=ExportPDFToImagesTargetFormat.JPEG,
export_pdf_to_images_output_type=ExportPDFToImagesOutputType.LIST_OF_PAGE_IMAGES,
)

# Creates a new job instance
export_pdf_to_images_job = ExportPDFtoImagesJob(
input_asset=input_asset,
export_pdf_to_images_params=export_pdf_to_images_params,
)

# Submit the job and gets the job result
location = pdf_services.submit(export_pdf_to_images_job)
pdf_services_response = pdf_services.get_job_result(
location, ExportPDFtoImagesResult
)

# Get content from the resulting asset(s)
result_assets = pdf_services_response.get_result().get_assets()

output_file_path = "output/ExportPDFToImages"

for (asset_index, asset) in enumerate(result_assets):
save_output_file_path = f"{output_file_path}_{asset_index}.jpeg"
stream_asset: StreamAsset = pdf_services.get_content(asset)
# Creates an output stream and copy stream asset's content to it
with open(save_output_file_path, "wb") as file:
file.write(stream_asset.get_input_stream())
except (ServiceApiException, ServiceUsageException, SdkException) as e:
logging.exception(f"Exception encountered while executing operation: {e}")
if __name__ == "__main__":
ExportPDFtoJPEG()
```
Loading

0 comments on commit 3b2e32e

Please sign in to comment.