Skip to content

Commit

Permalink
Add upload + raw JSON Python samples
Browse files Browse the repository at this point in the history
  • Loading branch information
datalogics-tsmith committed Oct 25, 2023
1 parent 2c63036 commit b462f9c
Show file tree
Hide file tree
Showing 31 changed files with 1,203 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Python/raw JSON calls/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
This directory contains examples of how to call each endpoint of the
PdfRest API using Python. Please refer to https://pdfrest.com/documentation.html
for accepted inputs, options, and other specifics about individual endpoints.

#### How to run Python samples

Using Python3:

1. `cd Python/`
2. `python name-of-sample-program.py`
39 changes: 39 additions & 0 deletions Python/raw JSON calls/bmp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
bmp_data = { "id" : uploaded_id }
print(json.dumps(bmp_data, indent = 2))


print("Processing file...")
bmp_response = requests.post(url='https://api.pdfrest.com/bmp',
data=json.dumps(bmp_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(bmp_response.status_code))
if bmp_response.ok:
bmp_response_json = bmp_response.json()
print(json.dumps(bmp_response_json, indent = 2))

else:
print(bmp_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/compressed-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
compress_data = { "id" : uploaded_id, "compression_level" : "low" }
print(json.dumps(compress_data, indent = 2))


print("Processing file...")
compress_response = requests.post(url='https://api.pdfrest.com/compressed-pdf',
data=json.dumps(compress_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(compress_response.status_code))
if compress_response.ok:
compress_response_json = compress_response.json()
print(json.dumps(compress_response_json, indent = 2))

else:
print(compress_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/decrypted-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
decrypt_data = { "id" : uploaded_id, 'current_open_password': 'password' }
print(json.dumps(decrypt_data, indent = 2))


print("Processing file...")
decrypt_response = requests.post(url='https://api.pdfrest.com/decrypted-pdf',
data=json.dumps(decrypt_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(decrypt_response.status_code))
if decrypt_response.ok:
decrypt_response_json = decrypt_response.json()
print(json.dumps(decrypt_response_json, indent = 2))

else:
print(decrypt_response.text)
else:
print(upload_response.text)
38 changes: 38 additions & 0 deletions Python/raw JSON calls/encrypted-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
encrypt_data = { "id" : uploaded_id, 'new_open_password': 'password' }
print(json.dumps(encrypt_data, indent = 2))

print("Processing file...")
encrypt_response = requests.post(url='https://api.pdfrest.com/encrypted-pdf',
data=json.dumps(encrypt_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(encrypt_response.status_code))
if encrypt_response.ok:
encrypt_response_json = encrypt_response.json()
print(json.dumps(encrypt_response_json, indent = 2))

else:
print(encrypt_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/exported-form-data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
export_data = { "id" : uploaded_id, 'data_format': 'xml' }
print(json.dumps(export_data, indent = 2))


print("Processing file...")
export_response = requests.post(url='https://api.pdfrest.com/exported-form-data',
data=json.dumps(export_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(export_response.status_code))
if export_response.ok:
export_response_json = export_response.json()
print(json.dumps(export_response_json, indent = 2))

else:
print(export_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/extracted-text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
extract_data = { "id" : uploaded_id }
print(json.dumps(extract_data, indent = 2))


print("Processing file...")
extract_response = requests.post(url='https://api.pdfrest.com/extracted-text',
data=json.dumps(extract_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(extract_response.status_code))
if extract_response.ok:
extract_response_json = extract_response.json()
print(json.dumps(extract_response_json, indent = 2))

else:
print(extract_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/flattened-annotations-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
flatten_data = { "id" : uploaded_id }
print(json.dumps(flatten_data, indent = 2))


print("Processing file...")
flatten_response = requests.post(url='https://api.pdfrest.com/flattened-annotations-pdf',
data=json.dumps(flatten_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(flatten_response.status_code))
if flatten_response.ok:
flatten_response_json = flatten_response.json()
print(json.dumps(flatten_response_json, indent = 2))

else:
print(flatten_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/flattened-forms-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
flatten_data = { "id" : uploaded_id }
print(json.dumps(flatten_data, indent = 2))


print("Processing file...")
flatten_response = requests.post(url='https://api.pdfrest.com/flattened-forms-pdf',
data=json.dumps(flatten_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(flatten_response.status_code))
if flatten_response.ok:
flatten_response_json = flatten_response.json()
print(json.dumps(flatten_response_json, indent = 2))

else:
print(flatten_response.text)
else:
print(upload_response.text)
39 changes: 39 additions & 0 deletions Python/raw JSON calls/flattened-layers-pdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import requests
import json

with open('/path/to/file', 'rb') as f:
upload_data = f.read()

print("Uploading file...")
upload_response = requests.post(url='https://api.pdfrest.com/upload',
data=upload_data,
headers={'Content-Type': 'application/octet-stream', 'content-filename': 'file.pdf', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})

print("Upload response status code: " + str(upload_response.status_code))

if upload_response.ok:
upload_response_json = upload_response.json()
print(json.dumps(upload_response_json, indent = 2))


uploaded_id = upload_response_json['files'][0]['id']
flatten_data = { "id" : uploaded_id }
print(json.dumps(flatten_data, indent = 2))


print("Processing file...")
flatten_response = requests.post(url='https://api.pdfrest.com/flattened-layers-pdf',
data=json.dumps(flatten_data),
headers={'Content-Type': 'application/json', "API-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"})



print("Processing response status code: " + str(flatten_response.status_code))
if flatten_response.ok:
flatten_response_json = flatten_response.json()
print(json.dumps(flatten_response_json, indent = 2))

else:
print(flatten_response.text)
else:
print(upload_response.text)
Loading

0 comments on commit b462f9c

Please sign in to comment.