Skip to content

Commit 4f294db

Browse files
python: Add TDM samples
Assisted-by: Codex
1 parent 77c462d commit 4f294db

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import requests
2+
import json
3+
4+
# This sample uploads a PDF and applies TDM rights metadata.
5+
6+
# By default, we use the US-based API service. This is the primary endpoint for global use.
7+
api_url = "https://api.pdfrest.com"
8+
9+
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
10+
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
11+
#api_url = "https://eu-api.pdfrest.com"
12+
13+
with open('/path/to/file', 'rb') as f:
14+
upload_data = f.read()
15+
16+
print("Uploading file...")
17+
upload_response = requests.post(url=api_url+'/upload',
18+
data=upload_data,
19+
headers={'Content-Type': 'application/octet-stream', 'Content-Filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})
20+
21+
print("Upload response status code: " + str(upload_response.status_code))
22+
23+
if upload_response.ok:
24+
upload_response_json = upload_response.json()
25+
print(json.dumps(upload_response_json, indent = 2))
26+
27+
28+
uploaded_id = upload_response_json['files'][0]['id']
29+
tdm_reserved_pdf_data = { "id" : uploaded_id, "policy": "https://example.com/tdm-policy" }
30+
print(json.dumps(tdm_reserved_pdf_data, indent = 2))
31+
32+
33+
print("Applying TDM rights metadata...")
34+
tdm_reserved_pdf_response = requests.post(url=api_url+'/tdm-reserved-pdf',
35+
data=json.dumps(tdm_reserved_pdf_data),
36+
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})
37+
38+
39+
40+
print("Processing response status code: " + str(tdm_reserved_pdf_response.status_code))
41+
if tdm_reserved_pdf_response.ok:
42+
tdm_reserved_pdf_response_json = tdm_reserved_pdf_response.json()
43+
print(json.dumps(tdm_reserved_pdf_response_json, indent = 2))
44+
45+
else:
46+
print(tdm_reserved_pdf_response.text)
47+
else:
48+
print(upload_response.text)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from requests_toolbelt import MultipartEncoder
2+
import requests
3+
import json
4+
5+
# This sample applies metadata on a PDF declaring Text and Data Mining (TDM) rights.
6+
7+
# By default, we use the US-based API service. This is the primary endpoint for global use.
8+
api_url = "https://api.pdfrest.com"
9+
10+
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
11+
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
12+
#api_url = "https://eu-api.pdfrest.com"
13+
14+
tdm_reserved_pdf_endpoint_url = api_url+'/tdm-reserved-pdf'
15+
16+
# The /tdm-reserved-pdf endpoint can take a single PDF file or id as input.
17+
mp_encoder_tdm_reserved_pdf = MultipartEncoder(
18+
fields={
19+
'file': ('file_name.pdf', open('/path/to/file', 'rb'), 'application/pdf'),
20+
'policy': 'https://example.com/tdm-policy',
21+
'output' : 'example_tdm_reserved_pdf_out'
22+
}
23+
)
24+
25+
# Let's set the headers that the tdm-reserved-pdf endpoint expects.
26+
# Since MultipartEncoder is used, the 'Content-Type' header gets set to 'multipart/form-data' via the content_type attribute below.
27+
headers = {
28+
'Accept': 'application/json',
29+
'Content-Type': mp_encoder_tdm_reserved_pdf.content_type,
30+
'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # place your api key here
31+
}
32+
33+
print("Sending POST request to tdm-reserved-pdf endpoint...")
34+
response = requests.post(tdm_reserved_pdf_endpoint_url, data=mp_encoder_tdm_reserved_pdf, headers=headers)
35+
36+
print("Response status code: " + str(response.status_code))
37+
38+
if response.ok:
39+
response_json = response.json()
40+
print(json.dumps(response_json, indent = 2))
41+
else:
42+
print(response.text)
43+
44+
# If you would like to download the file instead of getting the JSON response, please see the 'get-resource-id-endpoint.py' sample.

0 commit comments

Comments
 (0)