Copy link to clipboard
Copied
Hi Team, I need to create pdf from dynamic html.
So I have HTML file, inline CSS and json data.
How I can use new rest API's to create PDF.
I tried following steps -
1.Create a zip file from html, css and json file.
2.Generate access token.
2.Upload them to adobe cloud and get asset ID.
3.Call api- https://pdf-services-ue1.adobe.io/operation/htmltopdf
but it's not working.
Can any one help me on this? As new rest api documentation is not clear
Copy link to clipboard
Copied
Are you bundling the JSON in the ZIP? If you run the index.html file from localhost does it display properly?
Copy link to clipboard
Copied
Yes, I am builiding json in zip.
Yes, If I run index.html from localhost it displaying properly.
Copy link to clipboard
Copied
My steps -
Copy link to clipboard
Copied
Hi, Joel_Geraci, can you please check my comments and update as I have to complete this ASAP.
Copy link to clipboard
Copied
If you are bundling the JSON in the ZIP, you don't need the json parameter.
Copy link to clipboard
Copied
I am passing json inside json.js file. can you please tell me better way to do that or it's better if you provide me a example to create pdf from dynamic html.
Thanks
Copy link to clipboard
Copied
Hi Joel, Thanks for your response.
I didn't pass json parameter then I am getting response -
{'error': {'code': 'INVALID_ZIP', 'message': 'The zip file provided has invalid content.; requestId=2lFWxNqrJzC4zWxJ4hW3JCpf0eRPSN3l', 'status': 400}, 'status': 'failed'}
can you please provide valid content type? as it didn't mention clearly in documents.
Thanks
Copy link to clipboard
Copied
When creating a ZIP for a set of files, it's very common to select the folder and ZIP it. That's the exact wrong way to do it for this use case though. The index.html file needs to be at the top level of the ZIP file. To do this, do not select the folder. Instead, open the folder, select all of the folder contents, and compress that.
Copy link to clipboard
Copied
Yes Joel, I am creating zip in the same way (open the folder, select all of the folder contents, and compress) as you are telling as we were using same in older apis.
But I am getting content-type issue. As of now I am using content-type - application/json for all apis.
Can you tell me which content type should I use?
Copy link to clipboard
Copied
Hi @joel
I am using the file provided in github as example than also getting
{'error': {'code': 'INVALID_ZIP', 'message': 'The zip file provided has invalid content.; requestId=mLcoPS2zId7XPi6dwpsyJqd2thxxSG5Q', 'status': 400}, 'status': 'failed'}
can you please help me on this.
Copy link to clipboard
Copied
I think I see it now. In step 2, you may need to set the Content-Type to application/zip
Copy link to clipboard
Copied
Hi Joel,
Whenever I am trying to create dynamic html to pdf. I am getting bad address error.
Earliar it was working fine but now I am getting issue.
Whenever I am doing download polling to asset I am getting bad address Error.
{'error': {'code': 'BAD_ADDRESS', 'message': 'Bad Address, request terminated; requestId=xxxxxxxxxxxxxxxx', 'status': 400}, 'status': 'failed'}
Copy link to clipboard
Copied
What part of the process is giving you that error?
Copy link to clipboard
Copied
I uploaded asset. Now I am doing download polling to asset
https://pdf-services-ue1.adobe.io/operation/htmltopdf/<asset_id>/status
h = {
"Authorization": f"Bearer {access_token}",
"x-api-key": client_id}
getting response
{'error': {'code': 'BAD_ADDRESS', 'message': 'Bad Address, request terminated; requestId=kuoRG2ucAHYiBzuoSvebuKP3wKPTEvCv', 'status': 400}, 'status': 'failed'}
Copy link to clipboard
Copied
You don't use the assetId in the poll, but the job id.
Copy link to clipboard
Copied
Yes, it's job ID only @Raymond Camden .
Can you please tell me now? How I can resolve this error. As it's blocking us to generate reports
Copy link to clipboard
Copied
When you create the job, the result contains a Location header that is the complete job URL you can check.
Copy link to clipboard
Copied
yes, I am using same only. Pls check my code
response = requests.post('https://pdf-services.adobe.io/operation/htmltopdf', headers=headers, json=json_data)
print(response.headers['location'])
print(response.status_code)
h = {
"Authorization": f"Bearer {access_token}",
"x-api-key": client_id}
poll = True
while poll: # a loop constructed so as to write the pdf document only when its content is returned in the get response
get_resp = requests.get(response.headers['location'], headers=h)
print(get_resp.status_code)
print(get_resp.headers)
get_resp = get_resp.json()
print(get_resp)
if get_resp.get('status') == 'done': # the response contains output file content only if the status=200
print(f"HI")
print(get_resp.get('asset'))
download_uri = get_resp.get('asset')['downloadUri']
print(download_uri)
response = requests.get(download_uri)
with open('/home/shivj/enlume/projects/reports/latest/monthly_report.pdf_6', 'wb') as f:
f.write(response.content)
poll = False
else:
time.sleep(5)
Copy link to clipboard
Copied
@Chandrashekar32587445qwre you are new to this thread, I assume you get the same error? Can you share the error you get?
Copy link to clipboard
Copied
Myself and shekar from same team. we are facing same issue. Can you please help us to resolve this issue?
Copy link to clipboard
Copied
Show me what you get when you print response.headers['location'] and the error please.
Copy link to clipboard
Copied
When i am printing response.headers['location']
I am getting
https://pdf-services-ue1.adobe.io/operation/htmltopdf/yGUvrahzjKne6gYOmMkRUv5migYpBMoJ/status
Then I am calling this to check status so I am getting this
{'error': {'code': 'BAD_ADDRESS', 'message': 'Bad Address, request terminated; requestId=yGUvrahzjKne6gYOmMkRUv5migYpBMoJ', 'status': 400}, 'status': 'failed'}
Copy link to clipboard
Copied
Not sure to be honest. Can you share a GitHub gist with the complete script, minus credentials of course.
Copy link to clipboard
Copied
Hi, I attaching complete script. please check
token_url = 'https://pdf-services.adobe.io/token'
upload_assets_url = 'https://pdf-services.adobe.io/assets'
import datetime as dt
import requests
import json
import time
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
params = {
'client_id': client_id,
'client_secret': client_secret
}
r = requests.post(token_url, data=params, headers=headers).json()
access_token = r.get('access_token')
print(f"access_token : {access_token}")
headers = {'X-API-Key': client_id, 'Authorization': f"Bearer {access_token}", 'Content-Type': 'application/json'}
params = '{"mediaType": "application/zip"}'
asset_req = requests.post(upload_assets_url, data = params, headers=headers).json()
presigned_upload_uri = asset_req.get('uploadUri')
asset_id = asset_req.get('assetID')
headers = {'Content-Type': 'application/zip'}
with open('/home/shivj/Downloads/monthly_report_html.zip', 'rb') as f:
data = f.read()
r = requests.put(presigned_upload_uri, headers=headers, data = data)
print(r)
headers = {
'x-api-key': client_id,
'Content-Type': 'application/json',
'Authorization': f"Bearer {access_token}",
}
json_data = {
'assetID': asset_id,
'includeHeaderFooter': False,
'pageLayout': {
'pageWidth': 8.5,
'pageHeight': 12,
},
}
print(json_data)
response = requests.post('https://pdf-services.adobe.io/operation/htmltopdf', headers=headers, json=json_data)
print(response.headers['location'])
print(response.status_code)
h = {
"Authorization": f"Bearer {access_token}",
"x-api-key": client_id}
poll = True
while poll: # a loop constructed so as to write the pdf document only when its content is returned in the get response
get_resp = requests.get(response.headers['location'], headers=h)
print(get_resp.status_code)cd
print(get_resp.headers)
get_resp = get_resp.json()
print(get_resp)
if get_resp.get('status') == 'done': # the response contains output file content only if the status=200
print(f"HI")
print(get_resp.get('asset'))
download_uri = get_resp.get('asset')['downloadUri']
print(download_uri)
response = requests.get(download_uri)
with open('/home/shivj/enlume/projects/reports/latest/monthly_report.pdf_6', 'wb') as f:
f.write(response.content)
poll = False
else:
time.sleep(5)