Plutext

PDF Converter from Python

This page covers invoking the PDF Converter from Python.

To test your document online first, or to download an installer, please see here

The sample code below uses endpoint converter-eval.plutext.com; just change the URL to point at your local install.

Using urllib2

Here is sample client code:


import urllib2

# the input docx
file = open('example.docx', 'rb')
data = file.read()

#set up the request
req = urllib2.Request("http://converter-eval.plutext.com:80/v1/00000000-0000-0000-0000-000000000000/convert", data)
req.add_header('Content-Length', '%d' % len(data))
req.add_header('Content-Type', 'application/octet-stream')

# make the request
res = urllib2.urlopen(req)

# write the response to a file
pdf = res.read()
f = open('out.pdf', 'wb')
f.write(pdf)

Using requests

Here is sample client code:


import requests

# the input docx
file = open('example.docx', 'rb')
data = file.read()

#the request
res = requests.post(url='http://converter-eval.plutext.com:80/v1/00000000-0000-0000-0000-000000000000/convert',
                    data=data,
                    headers={'Content-Type': 'application/octet-stream'})


# write the response to a file
f = open('out_other.pdf', 'wb')
f.write(res.content)
    

or alternatively:


import requests

with open('example.docx', 'rb') as docx:
  res = requests.post(url='http://converter-eval.plutext.com:80/v1/00000000-0000-0000-0000-000000000000/convert',
                      data=docx,
                      headers={'Content-Type': 'application/octet-stream'})
  f = open('out_request.pdf', 'wb')
  f.write(res.content)