Getting started
Overview Supported languages File types Test mode Postman collection Zapier integration DWS MCP Server Tools and APIsPricing
Pricing Calculate credit usage Pricing per toolDeveloper guides
API overview Authentication Errors Combine workflows Performance PDF generation Web SDK client API referenceSupported languages
Java C# JavaScript Python PHP Other languages Deployment options Security Privacy Support About NutrientPython PDF API — Getting started
With Nutrient’s Python PDF API, you can quickly process your documents by making HTTP requests to one of our 50+ API tools. You can make a single request to one tool or combine API actions to generate, edit, OCR, and convert your document (1 document can have multiple API actions).
Using our Python SDK
For the best developer experience, we recommend using our official Python SDK library:
Installation
pip install nutrient-dws
# Enable enhanced code completion and documentation for Claude Code using the Nutrient DWS SDK
dws-add-claude-code-rule
# Enable enhanced code completion and documentation for GitHub Copilot using the Nutrient DWS SDK
dws-add-github-copilot-rule
# Enable enhanced code completion and documentation for Junie (Jetbrains) using the Nutrient DWS SDK
dws-add-junie-rule
# Enable enhanced code completion and documentation for Cursor using the Nutrient DWS SDK
dws-add-cursor-rule
# Enable enhanced code completion and documentation for Windsurf using the Nutrient DWS SDK
dws-add-windsurf-rule
Quick example
import asyncio
from nutrient_dws import NutrientClient
from nutrient_dws.builder.constant import BuildActions
async def main():
client = NutrientClient(api_key='your_api_key_here')
# Add an image watermark to a document.
image_result = await client.watermark_image('./document.pdf', './logo.png', {
'width': {'value': 50, 'unit': '%'}
})
with open('./watermarked-document.pdf', 'wb') as f:
f.write(image_result.buffer)
# Redact and archive a document set using chained workflow.
archived_result = await (client
.workflow()
# Add documents from multiple formats (PDF, DOCX, PNG).
.add_file_part("./form.docx")
.add_file_part("./policy.pdf")
.add_file_part("./image.png")
# Apply multiple actions: watermark and redactions using apply_actions
.apply_actions([
BuildActions.create_redactions_preset('email-address'),
BuildActions.create_redactions_preset('international-phone-number'),
BuildActions.create_redactions_preset('north-american-phone-number'),
BuildActions.apply_redactions(),
BuildActions.watermark_text('ARCHIVED', {
'opacity': 0.45,
'fontSize': 32,
'fontColor': '#FF0000',
'rotation': 45,
'fontStyle': ['bold']
}),
])
# Output as PDF/A format.
.output_pdfa({
'conformance': 'pdfa-2b',
'vectorization': True,
'metadata': {
'title': 'Multi-Format Merged Document',
'author': 'Archive.org',
},
'optimize': {
'mrcCompression': True,
'imageOptimizationQuality': 3
}
})
# Tracking progress.
.execute(on_process= lambda current, total: print(f"Processing step {current} of {total}")))
with open('./archived-document.pdf', 'wb') as f:
f.write(archived_result.buffer)
if __name__ == "__main__":
asyncio.run(main())
Alternative: Using raw HTTP requests
If you prefer to use raw HTTP requests or want to understand the underlying API calls, you can also interact with the API directly using any HTTP client.
This guide uses the requests
HTTP library. Keep in mind that you can use any HTTP client you want; this example uses requests
to demonstrate the principles of interacting with Nutrient DWS API.
Raw HTTP request example
This section will demonstrate how you can use Python to make HTTP requests with our API by:
-
1
-
2
-
3
Installing the required dependencies
First, you need to make sure you have all the dependencies set up:
python -m pip install requests
You’ll also need to add document.pdf
and logo.png
files to the root of your Python project (the same folder you’ll be creating the pspdfkit.py
file in). You can use the sample files provided by us — document.pdf and logo.png — or use your own.
Now that you have your dependencies and assets set up, you’re ready to start making requests to Nutrient DWS API.
First, create a pspdfkit.py
file, which will contain your code to call Nutrient DWS API. You can immediately import your dependencies as well:
import requests
import json
Next, you’ll prepare the payload.
Preparing the payload
Create your instructions
object:
instructions = {
'parts': [
{
'file': 'document'
}
],
'actions': [
{
'type': 'watermark',
'image': 'company-logo',
'width': '50%'
},
{
'type': 'watermark',
'text': 'Property of Nutrient',
'width': 150,
'height': 20,
'left': 0,
'bottom': '100%'
}
]
}
payload = {
'instructions': json.dumps(instructions)
}
Since Nutrient DWS API expects the instructions
to be a JSON object, you first create a dictionary that you then convert to a JSON object using the json.dumps
function. In this example, you’ll be adding an image watermark. For more details on the available options specifically related to watermarking, refer to our watermarking guide.
Next, you need to prepare your assets to be sent:
files = {
'document': open('document.pdf', 'rb'),
'company-logo': open('logo.png', 'rb')
}
The requests
library expects a file
object to be passed in. So, open your assets here.
Making the request
Finally, you’re ready to make your request. Make sure to replace the your_api_key_here
placeholder with your actual API key if it hasn’t been replaced yet:
headers = {
'Authorization': 'Bearer your_api_key_here'
}
response = requests.request(
'POST',
'https://api.nutrient.io/build',
headers=headers,
files=files,
data=payload,
stream=True
)
with open('result.pdf', 'wb') as fd:
for chunk in response.iter_content(chunk_size=8096):
fd.write(chunk)
This will make a request to Nutrient DWS API, send your previously defined instructions
object and your assets to the API, and save the resulting PDF as result.pdf
in the same folder as your Python file.
And with that, you’re ready to make a request. Run python pspdfkit.py
, and result.pdf
will show up next to your Python file.
While this example made use of our watermarking API, this same approach can be used for all our available API tools.
Full code
For your convenience, here’s the whole code. Just copy it and run it:
import requests
import json
instructions = {
'parts': [
{
'file': 'document'
}
],
'actions': [
{
'type': 'watermark',
'image': 'company-logo',
'width': '50%'
},
{
'type': 'watermark',
'text': 'Property of Nutrient',
'width': 150,
'height': 20,
'left': 0,
'bottom': '100%'
}
]
}
payload = {
'instructions': json.dumps(instructions)
}
headers = {
'Authorization': 'Bearer your_api_key_here'
}
files = {
'document': open('document.pdf', 'rb'),
'company-logo': open('logo.png', 'rb')
}
response = requests.request(
'POST',
'https://api.nutrient.io/build',
headers=headers,
files=files,
data=payload,
stream=True
)
with open('result.pdf', 'wb') as fd:
for chunk in response.iter_content(chunk_size=8096):
fd.write(chunk)