CODEMONDAY
Published in

CODEMONDAY

PDF Form Submission Acrobat to REST API Server

What this solution is for

Warning

Prepare the form

Common mistake

Content-Type: application/vnd.fdf
Content-disposition: inline

Popup the success message

Content-Type: application/vnd.fdf
Content-disposition: inline
Body: %FDF-1.2
1 0 obj
<<
/FDF
<<
/JavaScript
<<
/Doc 2 0 R
/After (confirmSend();)
>>
>>
>>
endobj
2 0 obj
[
(confirmSend) 3 0 R
]
endobj
3 0 obj
<<
>>
stream
function confirmSend()
{
app.alert({
cTitle : 'Submission Result',
cMsg : 'Your form name {{{first_name}}} client id {{{client_id}}} submission is success.',
nIcon : 3
});
}
endstream
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF

Bonus: My Python AWS Lambda REST Server

import json
import boto3
import csv
import urllib

BUCKET_NAME = 'my-demo'
OBJECT_NAME = 'sample/output.csv'
LAMBDA_LOCAL_TMP_FILE = '/tmp/test.csv'

FDF_RESPONSE_TEMPLATE = '''
%FDF-1.2
1 0 obj
<<
/FDF
<<
/JavaScript
<<
/Doc 2 0 R
/After (confirmSend();)
>>
>>
>>
endobj
2 0 obj
[
(confirmSend) 3 0 R
]
endobj
3 0 obj
<<
>>
stream
function confirmSend()
{
app.alert({
cTitle : 'Submission Result',
cMsg : 'Your form name {{{first_name}}} client id {{{client_id}}} submission is success.',
nIcon : 3
});
}
endstream
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
'''

def lambda_handler(event, context):

s3 = boto3.client('s3')
s3.download_file(BUCKET_NAME, OBJECT_NAME, LAMBDA_LOCAL_TMP_FILE)

body = urllib.parse.parse_qs(event['body'])
first_name = body['first_name'][0]
client_id = body['client_id'][0]
country = body['country'][0]

row = [first_name, client_id, country]

with open(LAMBDA_LOCAL_TMP_FILE, 'a') as f:
writer = csv.writer(f)
writer.writerow(row)

s3.upload_file(LAMBDA_LOCAL_TMP_FILE, BUCKET_NAME, OBJECT_NAME)

return {
'statusCode': 200,
'body': FDF_RESPONSE_TEMPLATE.replace('{{{first_name}}}', first_name).replace('{{{client_id}}}', client_id),
'headers': {
'Content-Type': 'application/vnd.fdf',
'Content-disposition' : 'inline'
}

}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store