Skip to main content
Participant
September 8, 2022
Question

Using the HTMltoPDF API with Python Requests

  • September 8, 2022
  • 0 replies
  • 252 views

Hi,

 

I'm trying to convert a local HTML file to a PDF using the htmlToPDF API.

 

I'm able to hit the API successfully using a CURL, but I'm running into a weird error when sending my request using the python.requests library:

{"cpf:status":{"completed":true,"type":"Invalid papi Engine","title":"Invalid engines input for papi request ","status":400,"report":"{\"error_code\":\"INVALID_REQUEST\"}"}}

Is there a successful POST implementation with a file I can mimic to help diagnose the issue? Here's my full code:

 

import requests
import json



BEARER="Bearer {{Token}}"
API_KEY = "{{client_secret}}"

requestUri = 'https://cpf-ue1.adobe.io/ops/:create?respondWith%3D%7B%22reltype%22%3A%20%22http%3A%2F%2Fns.adobe.com%2Frel%2Fprimary%22%7D'
headers = {
    'Authorization': BEARER,
    'x-api-key' : API_KEY,
    'Accept': "application/json, text/plain, */*",
    'Prefer': "respond-async,wait=0"
}





def makeRequest(inputHtmlFilePath, outputPdfFilePath):

    contentAnalyzerRequests = json.dumps({
        "cpfengine": {
            "repo:assetId": "urn:aaid:cpf:Service-e2ee120a2b06427cb449592f5db967e7"
        },
        "cpf:inputs": {
            "documentIn": {
                "dc:format": "text/html",
                "cpf:location": "InputFile0"
            }
        },
        "cpf:outputs": {
            "documentOut": {
                "dc:format": "application/pdf",
                "cpf:location": outputPdfFilePath
            }
        }
    })

    with open(inputHtmlFilePath, 'rb') as inFile:
        print(inFile.read)

        payload = {
            'contentAnalyzerRequests': contentAnalyzerRequests,
            'InputFile0': inFile
        }
    
        response = requests.post(requestUri, files=payload, headers=headers)
        print(response.text)
        print(response.json())

    return ()


makeRequest('out.html', 'testadobe.pdf')

 

This topic has been closed for replies.