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

Convert Dynamic HTML Resources to a PDF File using REST API

New Here ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

Error: 400 - Array ( [cpf:status] => Array ( [completed] => 1 [type] => Invalid papi Engine [title] => Invalid engines input for papi request [status] => 400 [report] => {"error_code":"INVALID_REQUEST"} ) )
hii i am using 
Convert HTML Resources to a PDF File which is not working.kindly help to resolve this issue 

<?php

// Function to get an access token from Adobe API
function getAccessToken($clientId, $clientSecret) {
$url = 'https://ims-na1.adobelogin.com/ims/token/v3';
$data = [
'grant_type' => 'client_credentials',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'scope' => 'openid'
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

$response = curl_exec($ch);
curl_close($ch);

$responseData = json_decode($response, true);
return $responseData['access_token'] ?? null;
}

// Function to convert HTML to PDF using Adobe's API
function convertHtmlToPdf($accessToken, $apiKey) {
$url = 'https://cpf-ue1.adobe.io/ops/:create';

// Prepare the HTML content
$htmlContent = '';

// Create a temporary file for the HTML content
$tempHtmlFile = tempnam(sys_get_temp_dir(), 'html_') . '.html';
file_put_contents($tempHtmlFile, $htmlContent);

// Create a ZIP file with the HTML
$zipFilePath = tempnam(sys_get_temp_dir(), 'html_to_pdf_') . '.zip';
$zip = new ZipArchive();
if ($zip->open($zipFilePath, ZipArchive::CREATE) === TRUE) {
$zip->addFile($tempHtmlFile, 'input.html');
$zip->close();
} else {
echo "Failed to create ZIP file.";
return;
}

// Prepare the payload as a JSON string
$payload = json_encode([
'contentAnalyzerRequests' => [
[
'cpf:engine' => [
'repo:assetId' => 'InputFile0', // Use placeholder for input file
],
'cpf:inputs' => [
'documentIn' => [
'cpf:location' => 'InputFile0', // Input file name
'dc:format' => 'application/zip', // Input format
],
'params' => [
'cpf:inline' => [
'print' => [
'includeHeaderFooter' => true,
],
'pageLayout' => [
'pageHeight' => 8.5,
'pageWidth' => 11,
],
],
],
],
'cpf:outputs' => [
'documentOut' => [
'cpf:location' => 'multipartLabelOut',
'dc:format' => 'application/pdf',
],
],
],
],
]);

// Prepare the cURL request
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $accessToken",
"x-api-key: $apiKey",
]);

// Use CURLFile to add the ZIP file and the JSON payload
$postFields = [
'InputFile0' => new CURLFile($zipFilePath),
'contentAnalyzerRequests' => $payload // Pass the JSON payload as a string
];

curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

// Execute the request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($httpCode === 200) {
// Check if the response is PDF data
if (strpos($response, '%PDF-') === 0) {
// Save PDF to the specified directory
$outputPdfPath = 'F:/xampp/htdocs/pdf/output.pdf';
file_put_contents($outputPdfPath, $response);
echo "PDF generated successfully. <a href='pdf/output.pdf'>Download PDF</a>";
} else {
echo "Error: Unexpected response format.";
}
} else {
// Handle the error response
$errorDetails = json_decode($response, true);
echo "Error: " . $httpCode . " - " . print_r($errorDetails, true);
}

// Clean up temporary files
unlink($tempHtmlFile);
unlink($zipFilePath);
}

// Your Adobe API credentials
$clientId = 'id value'; // Replace with your Client ID
$clientSecret = 'secret'; // Replace with your Client Secret
$apiKey = 'api key'; // Replace with your API Key

// Get access token
$accessToken = getAccessToken($clientId, $clientSecret);
if ($accessToken) {
// Convert HTML to PDF
convertHtmlToPdf($accessToken, $apiKey);
} else {
echo "Failed to generate access token.";
}
?>

TOPICS
PDF Services API , REST APIs

Views

1.4K

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 ,
Oct 16, 2024 Oct 16, 2024

Copy link to clipboard

Copied

Hi, first off, I edited your message to remove your credentials. Please do not post credentials in a public forum.

 

Can you explain _where_ you are getting the error? Is your call to get an access token working for example? Are you able to upload the zip ok, and so forth?

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
New Here ,
Oct 17, 2024 Oct 17, 2024

Copy link to clipboard

Copied

i am trying convert html into pdf it doesnt render css and design as in html also add multiple break in pages.please help me sort out 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
New Here ,
Oct 17, 2024 Oct 17, 2024

Copy link to clipboard

Copied

{
  "json": "{}",
  "includeHeaderFooter": false,
  "pageLayout": {
    "pageWidth": 12.40,
    "pageHeight": 7.50
  },
  "notifiers": [
    {
      "type": "CALLBACK",
      "data": {
        "url": "https://dummy.callback.org/",
        "headers": {
          "x-api-key": "dummykey",
          "access-token": "dummytoken"
        }
      }
    }
  ]
}
i am using this url for conversion . can you guide how this will work same for exact output.thank you support.please help me to figure out 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 ,
Oct 17, 2024 Oct 17, 2024

Copy link to clipboard

Copied

So first off, remove the notifiers part. You use that when you want to use the webhook feature, so your code there, using dummy values, doesn't make sense. Looking up at your first block of code, which is hard to read, it looks like you are working with a zip too? To be clear, you either upload a zip first, and process that using assetID, _or_ you use a url. You can't do both at the same time.

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
New Here ,
Oct 17, 2024 Oct 17, 2024

Copy link to clipboard

Copied

hi earlier i am working zip which is not working than i start working with input url which is working convert my html into pdf but no render any css design in my pdf i am using restapi also try to do with postman but it's not working with my requirement .is this support css and effects with display if it can kindly guide how i can get response according to pdf link i had attach earlier.i hope you undersatnd my requirement and give me some solution to reslove 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 ,
Oct 18, 2024 Oct 18, 2024

Copy link to clipboard

Copied

It's very difficult to understand your question. Initially you said you were getting an error. Now you are saying the API call *works*, but the result isn't what you would expect. Is that fair? Can you share the PDF you had generated?

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
New Here ,
Oct 21, 2024 Oct 21, 2024

Copy link to clipboard

Copied

i had attach my file .kindly review 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
New Here ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

hiii ,
thank you foe support while i am trying to convert html into pdf it page layout changes are not working. i want landscape view pdf it getting default. 
curl --location
'https://pdf-services-ue1.adobe.io/operation/htmltopdf' \
--header 'x-api-key: 892' \
--header 'Content-Type: application/json' \
--header 'Authorization: ••••••' \
--data '{
  "json": "{}",
  "includeHeaderFooter": false,
    "pageLayout": {
        "pageWidth": 12.40,
        "pageHeight": 7.50
    },
  "notifiers": [
    {
      "type": "CALLBACK",
      "data": {
        "url": "https://dummy.callback.org/",
        "headers": {
          "x-api-key": "dummykey",
          "access-token": "dummytoken"
        }
      }
    }
  ]
}'
i also attached my pdf file for referenc to my issue.Kindly check this and give some support to handle 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 ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

First off, please remove notifiers. You are passing default values there that don't work. It won't break the API or anything, but the less unnecessary code the better. 

When I look at your PDF, I'm seeing wide pages, so landscape mode. Are you not seeing 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
New Here ,
Oct 22, 2024 Oct 22, 2024

Copy link to clipboard

Copied

hii i had seen but you didn't notice i was mentionated regarding 

  "pageLayout": {
        "pageWidth"12.40,
        "pageHeight"7.50
    }, 
 which pdf is generating which getting differnt page size with potrait mode not in landscape.it's usually happen when i am using bootsrap link if i was removing than 50% of css comes with design but it add blank space between pages header and body content.what should i need to do render my complete css with design as in my html
https://www.costmasters.in/report_January_2024.html
Kindly provide me solution as soon as possible 
--header 'x-api-key: b33a4d8179e64919827bc27fea399b9a' \
--header 'Content-Type: application/json' \
--header 'Authorization: ••••••' \
--data '{
  "json": "{}",
  "includeHeaderFooter": false,
    "pageLayout": {
        "pageWidth": 12.40,
        "pageHeight": 7.50
    },
  "notifiers": [
  ]
}'

 

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 ,
Oct 24, 2024 Oct 24, 2024

Copy link to clipboard

Copied

I'm sorry I'm really struggling to understand you. You said... you are getting page with different sizes, it's not matching the layout exactly? 

 

What do you see if you print to PDF from the browser?

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
New Here ,
Oct 24, 2024 Oct 24, 2024

Copy link to clipboard

Copied

kindly delete my pdf from msg i am unable to edit or delete it.i just want to remove the white margin which is adding on every page marging from left right top and bottom.what is the synatx to remove this like to set the image on complete screen 

 

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
New Here ,
Oct 24, 2024 Oct 24, 2024

Copy link to clipboard

Copied

LATEST

Hi,

 

I tried to use Adobe's document service API to convert HTML to PDF. But the generated pdf always has a white margin around each page. I don't see any options to scale the HTML to the whole page. I tried the PageLayout options, where you can specify height and weight, but it won't remove the margin.

 

Is there any way to remove the while margin. Otherwise this tool won't satisfy our requirements.

 

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