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

Create PDF from dynamic HTML

Community Beginner ,
Aug 21, 2023 Aug 21, 2023

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

TOPICS
PDF Services API

Views

1.9K

Translate

Translate

Report

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

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

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

 

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

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?

 

Votes

Translate

Translate

Report

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

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.

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

What part of the process is giving you that error?

Votes

Translate

Translate

Report

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

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


Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

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. 

Votes

Translate

Translate

Report

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

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)

 

Votes

Translate

Translate

Report

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

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?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Hi Raymond Camden,

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

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)

Votes

Translate

Translate

Report

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