forked from pdfrest/pdfrest-api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-convert-to-markdown.py
More file actions
133 lines (111 loc) · 5.05 KB
/
Copy pathprepare-convert-to-markdown.py
File metadata and controls
133 lines (111 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from requests_toolbelt import MultipartEncoder
import requests
import json
# This sample demonstrates a workflow to prepare form and image-only documents
# for conversion to Markdown. This process uses the Query PDF tool to determine
# whether the document contains forms or contains only images. Form documents
# have their forms flattened, and image-only documents are processed through
# the OCR PDF tool.
#
# To get started, configure the following constants.
# By default, we use the US-based API service. This is the primary endpoint for global use.
API_URL = "https://api.pdfrest.com"
# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#API_URL = "https://eu-api.pdfrest.com"
API_KEY = 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' # Place your api key here.
OCR_LANGUAGES = "English" # a comma-separated list of languages for the OCR tool to use
INPUT_FILE_LOCATION = '/path/to/file/location/' # Replace this value with the home directory of your input PDF.
INPUT_FILE_NAME = 'myFile.pdf' # Replace this value with the filename of your input PDF.
DO_OCR = True # If True, this sample will perform OCR on PDFs that are image-only.
DO_FORM_FLATTENING = True # If True, this sample will flatten forms in form documents.
pdf_endpoint_url = f"{API_URL}/pdf-info"
mp_encoder_query = MultipartEncoder(
fields={
'file': (INPUT_FILE_NAME, open(f"{INPUT_FILE_LOCATION}{INPUT_FILE_NAME}", 'rb'), 'application/pdf'),
'queries': "image_only,contains_xfa,contains_acroforms",
}
)
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_query.content_type,
'Api-Key': API_KEY
}
print("Sending POST request to Query PDF endpoint...")
response = requests.post(pdf_endpoint_url, data=mp_encoder_query, headers=headers)
print("Response status code: " + str(response.status_code))
if response.ok:
query_response = response.json()
if query_response["allQueriesProcessed"]:
pdf_id = query_response["inputId"]
# NOTE For demo purposes
is_starter_key = "message" in query_response
if is_starter_key:
pdf_contains_forms = query_response["contains_xfa"] == "tr**" or query_response["contains_acroforms"] == "tr**"
image_only_pdf = query_response["image_only"] == "tr**"
else:
pdf_contains_forms = query_response["contains_xfa"] or query_response["contains_acroforms"]
image_only_pdf = query_response["image_only"]
if pdf_contains_forms and DO_FORM_FLATTENING:
mp_encoder_flatten = MultipartEncoder(
fields={
'id': pdf_id,
}
)
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_flatten.content_type,
'Api-Key': API_KEY
}
flatten_endpoint_url = f"{API_URL}/flattened-forms-pdf"
print("Sending POST request to Flatten Forms endpoint...")
response = requests.post(flatten_endpoint_url, data=mp_encoder_flatten, headers=headers)
if response.ok:
flatten_response = response.json()
pdf_id = flatten_response["outputId"]
else:
print(response.text)
elif image_only_pdf and DO_OCR:
mp_encoder_ocr = MultipartEncoder(
fields={
'id': pdf_id,
'languages': OCR_LANGUAGES,
}
)
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_ocr.content_type,
'Api-Key': API_KEY
}
ocr_endpoint_url = f"{API_URL}/pdf-with-ocr-text"
print("Sending POST request to OCR PDF endpoint...")
response = requests.post(ocr_endpoint_url, data=mp_encoder_ocr, headers=headers)
if response.ok:
ocr_response = response.json()
pdf_id = ocr_response["outputId"]
else:
print(response.text)
mp_encoder_markdown = MultipartEncoder(
fields={
'id': pdf_id,
'output_type': "file", # Set to 'file' to get a download link to the .MD file.
}
)
headers = {
'Accept': 'application/json',
'Content-Type': mp_encoder_markdown.content_type,
'Api-Key': API_KEY
}
print("Sending POST request to Markdown endpoint...")
markdown_endpoint_url = f"{API_URL}/markdown"
response = requests.post(markdown_endpoint_url, data=mp_encoder_markdown, headers=headers)
print("Response status code: " + str(response.status_code))
if response.ok:
query_response = response.json()
print(json.dumps(query_response, indent = 2))
else:
print(response.text)
else:
print(response.text)
else:
print(response.text)