This code converts an url to pdf in Python using SelectPdf HTML To PDF REST API through a POST request. The parameters are JSON encoded. The content is saved into a file on the disk.
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 | # This code converts an url to pdf in Python using SelectPdf REST API through a POST request.
# The parameters are JSON encoded.
# The content is saved into a file on the disk.
import json
import urllib2
api_endpoint = 'http://selectpdf.com/api2/convert/'
key = 'your license key here'
test_url = 'http://selectpdf.com'
local_file = 'test.pdf'
# parameters - add here any needed API parameter
parameters = {
'key': key,
'url': test_url
}
requesturl = api_endpoint
print "Calling {0}\n".format(requesturl)
try:
request = urllib2.Request(requesturl)
request.add_header('Content-Type', 'application/json')
result = urllib2.urlopen(request, json.dumps(parameters))
localFile = open(local_file, 'wb')
localFile.write(result.read())
localFile.close()
print "Test pdf document generated successfully!"
except urllib2.HTTPError as e:
print "HTTP Response Code: {0}\nHTTP Response Message: {1}".format(e.code, e.reason)
except:
print "An error occurred!"
|
More Python samples for SelectPdf Online API, here: http://selectpdf.com/web-html-to-pdf-rest-api-for-python-samples/