Skip to main content
Participating Frequently
October 16, 2024
Question

Convert Dynamic HTML Resources to a PDF File using REST API

  • October 16, 2024
  • 1 reply
  • 4786 views

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.";
}
?>

This topic has been closed for replies.

1 reply

Raymond Camden
Community Manager
Community Manager
October 16, 2024

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?

Participating Frequently
October 17, 2024

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 

Participating Frequently
October 17, 2024
{
  "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