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.";
}
?>
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
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.
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?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
hii i had seen but you didn't notice i was mentionated regarding
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?
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
Copy link to clipboard
Copied
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.