Issue
I'm testing the ConvertAPI PDF to PDF/A but I have some problems. Unfortunately I cannot include the ready-made PHP client in my PHP project so I'm trying to call the API with curl from PHP.
However it always returns me http code 500
This is my code:
$formData = array(
'File' => '@' . trim($hdd_path . $_file_name),
'StoreFile' => true,
);
$url_conv_pdfa_convertapi_com = 'https://v2.convertapi.com/convert/pdf/to/pdfa?Secret=' . getConfig('external_services:convertapi_com:secret');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_conv_pdfa_convertapi_com);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
$result = curl_exec($ch);
HTTP/1.1 500 Internal Server Error
content-type: application/json; charset=utf-8
date: Mon, 29 Jan 2024 15:04:18 GMT
server: kaulas
cara: eu-hw3-CARA-VM 1.2.1.124 cara-589585cb64-lxpkq
x-envoy-upstream-service-time: 116
content-security-policy: default-src 'none'
strict-transport-security: max-age=31536000; includeSubDomains
x-content-type-options: nosniff
vary: Accept-Encoding
transfer-encoding: chunked
{"Code":5009,"Message":"File id is not valid."}
I expected file conversion to happen.
Solution
If you want to skip the PHP library and use your vanilla PHP with CURL
to convert a PDF to a PDF/A using COnvertAPI, you will need to follow these steps:
Get an API Key
: First, you need to sign up on ConvertAPI and obtain an API key.
Install Curl for PHP
: Make sure that you have curl enabled in your PHP installation to make HTTP requests.
Write the PHP Code
: Below is a basic example of PHP code to use ConvertAPI's PDF to PDF/A conversion service:
<?php
$apiKey = 'YOUR_API_KEY'; // Replace with your API key
$file = 'path_to_your_pdf_file.pdf'; // Replace with the path to your PDF file
$url = 'https://v2.convertapi.com/convert/pdf/to/pdfa?Secret=' . $apiKey;
$postData = [
'file' => new CURLFile($file),
'StoreFile' => true // Set to true if you want to store the file on ConvertAPI server
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if ($response) {
$result = json_decode($response, true);
if (isset($result['Files'])) {
$downloadUrl = $result['Files'][0]['Url'];
echo "Conversion successful. Download URL: " . $downloadUrl . "\n";
} else {
echo "Error in conversion: " . $response . "\n";
}
} else {
echo "Curl error: " . curl_error($ch) . "\n";
}
?>
This script does the following:
- It sends a POST request to ConvertAPI's conversion endpoint with your PDF file.
- The CURLFile class is used to upload the file in a multipart/form-data request.
- It then checks the response for a file URL and prints it out.
Remember to replace 'YOUR_API_KEY' with your actual API key and 'path_to_your_pdf_file.pdf' with the path to the PDF file you want to convert.
Before running the script, ensure that your PHP setup is configured correctly with cURL and that you have the necessary permissions to access and modify the files you're working with.
Answered By - Tomas Answer Checked By - Clifford M. (WPSolving Volunteer)