0

I'm pretty much a noob with Python (and coding in general), so please excuse me if I'm being stupid.

I'm writing a short script for a custom Zapier step, which is supposed to iterate through a list of URLs, pick the ones that end in .pdf and send those to ConvertAPI in order to be converted to JPGs.

Sending the request to ConvertAPI works so far and ConvertAPI says that the test file has been converted. Here's my question: How do I get the resulting URL of the converted file back? If I print the response, I get Response [200], but nothing else to work with.

I have tried turning on the Async parameter, but so far to no avail. From what I understand, StoreFile has to be set to true, but it doesn't seem to make a difference.

import requests
import json

url = 'https://v2.convertapi.com/convert/pdf/to/jpg?Secret=******' # Hidden
headers = {'content-type': 'application/json'}
payload = {
    'Parameters': [
        {
            'Name': 'File',
            'FileValue': {
                'Url': 'to be populated'
            }
        },
        {
            'Name': 'StoreFile',
            'Value': 'true'
        }
    ]
}

a = ['https://www.bachmann.com/fileadmin/02_Produkte/03_Anschlussfelder/CONI/Downloads/CONI_3-4-6-way_Mounting_instructions_REV05.pdf','test2.jpg','test3.jpeg','test4.png','test4.exe']

for x in a:

  if x[-3:] == 'pdf':
    payload['Parameters'][0]['FileValue']['Url'] = x
    response = requests.post(url, data=json.dumps(payload), headers=headers)
    print(response)

  elif x[-3:] == 'jpg' or x[-3:] == 'png' or x[-4:] == 'jpeg':
    print('thats an image, nothing to do here')
Tomas
  • 17,551
  • 43
  • 152
  • 257

3 Answers3

1

A friend helped me, with this IRL, here it goes:

import requests
import json

output = {'output_urls' : []} 
url = 'https://v2.convertapi.com/convert/pdf/to/jpg?Secret=xxxxxxx' # Hidden
headers = {'content-type': 'application/json'}
payload = {
    'Parameters': [
        {
            'Name': 'File',
            'FileValue': {
                'Url': 'to be populated'
            }
        },
        {
            'Name': 'StoreFile',
            'Value': 'true'
        },
        {
            'Name': 'ScaleImage',
            'Value': 'true'
        },
        {
            'Name': 'ScaleProportions',
            'Value': 'true'
        },
        {
            'Name': 'ScaleIfLarger',
            'Value': 'true'
        },
        {
            'Name': 'ImageHeight',
            'Value': '2200'
        },
        {
            'Name': 'ImageWidth',
            'Value': '1625'
        }
    ]
}

for x in input_data['input_urls'].split(',') : # input_data is passed by Zapier
  if x[-3:] == 'pdf':
    payload['Parameters'][0]['FileValue']['Url'] = x

    response = requests.post(url, data=json.dumps(payload), headers=headers)
    response_obj = json.loads(response._content)

    for file_url in response_obj['Files'] :
      output['output_urls'].append(file_url['Url'])

  elif x[-3:] == 'jpg' or x[-3:] == 'png' or x[-4:] == 'jpeg' :
    output['output_urls'].append(x)

return output
0
print(response)

is receive the status code of response so it's received 200 which mean the request is successfully

to get the url you can use .url

print(response.url)
Ahmed
  • 414
  • 4
  • 3
  • Thanks for the reply. print(response.url) returns https://v2.convertapi.com/convert/pdf/to/jpg?Secret=******* which is the URL the response seems to be coming from. But what I would like to get is the URL of the converted file, which would look something like https://v2.convertapi.com/job/d3bd2056-4330-4cf3-9b18-483a2412dd6b – Manuel Imboden Jan 18 '19 at 20:45
0

The ConvertAPI has Python library https://github.com/ConvertAPI/convertapi-python which would help you easily convert pdf to jpg using code below.

import convertapi
import os
import tempfile

convertapi.api_secret = os.environ['CONVERT_API_SECRET'] # your api secret

jpg_result = convertapi.convert(
    'jpg',
    {
        'File': 'files/test.pdf',
        'ScaleImage': True,
        'ScaleProportions': True,
        'ImageHeight': 300,
        'ImageWidth': 300,
    }
)

saved_files = jpg_result.save_files(tempfile.gettempdir())

print("The thumbnail saved to %s" % saved_files)
Tomas
  • 17,551
  • 43
  • 152
  • 257
  • Thanks for your response. This would surely be the best way to do it, but I'm writing this as a Zapier code step, which doesn't allow external libraries. From their docs: "Only the standard Python library and requests is available in the Code app and requests is already included in the namespace." (https://zapier.com/help/code-python/#requiring-or-using-external-libraries) – Manuel Imboden Jan 19 '19 at 08:40