pythonjsonzapierconvertapi

ConvertAPI: PDF to JPEG in Python 2.7.10 - how to get the conversion result URL?


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')

Solution

  • 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