Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Create PDF from dynamic HTML

Community Beginner ,
Aug 21, 2023 Aug 21, 2023

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

TOPICS
PDF Services API
3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 21, 2023 Aug 21, 2023

Are you bundling the JSON in the ZIP? If you run the index.html file from localhost does it display properly?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 21, 2023 Aug 21, 2023

Yes, I am builiding json in zip.
Yes, If I run index.html from localhost it displaying properly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 21, 2023 Aug 21, 2023

My steps - 

1.To get access token
 
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'client_id=<client_id>' \
--data-urlencode 'client_secret=<client_secret>'
 
resp - it is returning access-token
 
2.To get upload URI and asset id
 
curl --location --request POST 'https://pdf-services.adobe.io/assets' \
--header 'X-API-Key: <client_id>' \
--header 'Authorization: Bearer <access_token>' \
--header 'Content-Type: application/json' \
--data-raw '{
    "mediaType": "application/json"
}'
 
resp - upload_uri and assest_id
 
3.to upload file
 
curl -i --location -g --request PUT '<presigned upload url>' \
--header 'Content-Type: application/json' \
--data-binary '<zip_file_path>'
 
resp - status - 200
 
4.To create pdf
 
curl -i --location --request POST 'https://pdf-services.adobe.io/operation/htmltopdf' \
--header 'x-api-key: <client_id>' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <access_token>' \
--data-raw '{
    "assetID": "<assest_id>",
    "json": "{}"
    "includeHeaderFooter": true,
    "pageLayout": {
        "pageWidth": 11,
        "pageHeight": 8.5
    }
}'
 
 
resp - HTTP/1.1 400 Bad Request
{"reason":"Validation error Request id: wdqkjdc.","message":"Bad Request"}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 22, 2023 Aug 22, 2023

Hi, Joel_Geraci, can you please check my comments and update as I have to complete this ASAP.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 22, 2023 Aug 22, 2023

If you are bundling the JSON in the ZIP, you don't need the json parameter. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 23, 2023 Aug 23, 2023

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 23, 2023 Aug 23, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2023 Aug 24, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 24, 2023 Aug 24, 2023

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?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Aug 29, 2023 Aug 29, 2023

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 31, 2023 Aug 31, 2023

I think I see it now. In step 2, you may need to set the Content-Type to application/zip

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 09, 2024 Jul 09, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 09, 2024 Jul 09, 2024

What part of the process is giving you that error?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 09, 2024 Jul 09, 2024

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


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 09, 2024 Jul 09, 2024

You don't use the assetId in the poll, but the job id. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 09, 2024 Jul 09, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 10, 2024 Jul 10, 2024

When you create the job, the result contains a Location header that is the complete job URL you can check. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 10, 2024 Jul 10, 2024

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)

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 10, 2024 Jul 10, 2024

@Chandrashekar32587445qwre you are new to this thread, I assume you get the same error? Can you share the error you get?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 10, 2024 Jul 10, 2024

Hi Raymond Camden,

Myself and shekar from same team. we are facing same issue. Can you please help us to resolve this issue?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 10, 2024 Jul 10, 2024

Show me what you get when you print response.headers['location'] and the error please.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 10, 2024 Jul 10, 2024

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'}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Jul 10, 2024 Jul 10, 2024

Not sure to be honest. Can you share a GitHub gist with the complete script, minus credentials of course. 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 10, 2024 Jul 10, 2024

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)
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources