Use DocsFold PDF generation API with PHP

This code makes a POST request to the DocsFold API with the required headers and request body to generate a PDF file from the specified template and overrides.

You need to replace YOUR_API_KEY with your actual API key and YOUR_TEMPLATE_ID with your actual template ID.

This code example includes parsing the JSON response and downloading the PDF file from the specified URL. The PDF file is then saved to disk with the name "invoice.pdf".

Note that you'll need to adjust the file path as needed for the output PDF.

PHP Code Example using cURL

php
<?php
$api_key = 'YOUR_API_KEY';
$template_id = 'YOUR_TEMPLATE_ID';
$endpoint = 'https://app.docsfold.com/api/v1/generate-pdf';
$data = array(
'template' => $template_id,
'output_format' => 'url',
'overrides' => array(
'invoice_nr' => '123',
'created_date' => 'January 1, 2022',
'due_date' => 'February 1, 2022',
'company_name' => 'Acme Corp',
'company_address' => 'Acme road, 1',
'company_postal_code' => 'Sunnyville, 1000',
'client_company_name' => 'Doe Corp',
'client_name' => 'John Doe',
'client_email' => 'john@doe.com',
'payment_method' => 'Check',
'payment_method_value' => 1000,
'items' => array(
array(
'name' => 'Website design',
'value' => 300
),
array(
'name' => 'Hosting (3 months)',
'value' => 75
),
array(
'name' => 'Domain name (1 year)',
'value' => 10
)
)
)
);
$headers = array(
'Authorization: Bearer ' . $api_key,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
// Check if cURL request was successful
if ($response === false) {
echo 'Error: ' . curl_error($ch);
} else {
// Get HTTP response code
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo "Response: " . $response;
// Parse JSON response
$json = json_decode($response, true);
if ($json['status'] == 'SUCCESS') {
$pdfUrl = $json['pdf_url'];
$pdfData = file_get_contents($pdfUrl);
file_put_contents('invoice.pdf', $pdfData);
} else {
$errorMessage = $json['message'];
echo "Error: $http_code - $errorMessage";
}
}
curl_close($ch);
?>

PHP Code Example using file_get_contents

php
<?php
// Docsfold API endpoint URL
$url = "https://app.docsfold.com/api/v1/generate-pdf";
// API key for authentication
$api_key = "<YOUR_API_KEY>";
$template_id = "<YOUR_TEMPLATE_ID>";
// Request body as a JSON object
$data = array(
"template" => $template_id,
"output_format" => "url",
"overrides" => array(
"invoice_nr" => "123",
"created_date" => "January 1, 2022",
"due_date" => "February 1, 2022",
"company_name" => "Acme Corp",
"company_address" => "Acme road, 1",
"company_postal_code" => "Sunnyville, 1000",
"client_company_name" => "Doe Corp",
"client_name" => "John Doe",
"client_email" => "john@doe.com",
"payment_method" => "Check",
"payment_method_value" => 1000,
"items" => array(
array("name" => "Website design", "value" => 300),
array("name" => "Hosting (3 months)", "value" => 75),
array("name" => "Domain name (1 year)", "value" => 10)
)
)
);
// Convert the data to JSON format
$data_json = json_encode($data);
// Set up the request headers
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer " . $api_key
);
// Set up the request options
$options = array(
"http" => array(
"header" => implode("\r\n", $headers),
"method" => "POST",
"content" => $data_json
)
);
// Send the request and get the response
$response = file_get_contents($url, false, stream_context_create($options));
if ($response === false) {
echo 'Error: ' . var_dump($http_response_header);
} else {
// Parse the JSON response
$result = json_decode($response, true);
// Check if the PDF was generated successfully
if ($result["status"] === "SUCCESS") {
// Get the PDF URL and download the file
$pdf_url = $result["pdf_url"];
$pdf_data = file_get_contents($pdf_url);
file_put_contents("invoice.pdf", $pdf_data);
} else {
// Handle the error
$error_message = $result["message"];
echo "Error: $error_message";
}
}
?>
Author Image
About the author

Gustavo Santos

Gustavo is the founder of DocsFold, based in Portugal. He has worked as a software developer for over 15 years. He enjoys exploring new technologies and using them to automate work as much as possible.