In this tutorial, we will learn how to generate Word or PDF documents using the Documentero API service in PHP. Documentero is an API service that allows you to generate documents based on templates and data provided.

Prerequisites

Before we begin, make sure you have the following:

  • PHP installed on your machine
  • Documentero API access and your API secret key

Step 1: Setting up the Project

Let’s start by setting up a new PHP project. Open your preferred text editor or IDE and follow these steps:

  1. Create a new project directory for your PHP script.

  2. Open a terminal or command prompt and navigate to the project directory.

Step 2: Making a POST Request to Documentero API

Next, we’ll write the code to make a POST request to the Documentero API service and generate a document.

In your project directory, create a new PHP file, e.g., documentero_demo.php, and open it in your text editor or IDE.

Now, let’s define a function to generate the document:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

function generateDocument() {
$documenteroEndpoint = 'https://app.documentero.com/api';

$apiKey = 'YOUR_API_KEY'; // Replace with your API secret key
$templateIdentifier = 'Pt04DVNbifqY4inserI2'; // Replace with your template identifier
$format = 'docx'; // Set the desired format: 'docx' for Word or 'pdf' for PDF
$data = array(
'field' => 'fieldvalue' // Replace with the data you want to pass to the document generation
);

$requestData = array(
'document' => $templateIdentifier,
'apiKey' => $apiKey,
'format' => $format,
'data' => $data
);

$requestBody = json_encode($requestData);

$headers = array(
'Content-Type: application/json'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $documenteroEndpoint);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

$responseData = json_decode($response, true);

$status = $responseData['status'];
$message = $responseData['message'];
$downloadLink = $responseData['data'];

if ($status == 200) {
echo 'Document Generated Successfully' . PHP_EOL;
echo 'Download Link: ' . $downloadLink . PHP_EOL;
} else {
echo 'Document Generation Failed: ' . $message . PHP_EOL;
}
}

generateDocument();

Make sure to replace 'YOUR_API_KEY' with your actual API secret key obtained from Documentero, and 'Pt04DVNbifqY4inserI2' with the template identifier provided by Documentero.

Step 3: Running the Code

Save the changes to the documentero_demo.php file. In your terminal or command prompt, navigate to the project directory.

To execute the code and generate the document, run the following command:

1
php documentero_demo.php

If everything is set up correctly, you should see the following output:

1
2
Document Generated Successfully
Download Link: [ExpirableLinkToDownloadDocument]

The [ExpirableLinkToDownloadDocument] will be the actual link to download the generated

document, which can be either a Word document (.docx) or a PDF file (.pdf).

Congratulations! You have successfully generated a document using the Documentero API service in PHP.

Conclusion

In this tutorial, we learned how to generate Word or PDF documents in PHP using the Documentero API service. We made a POST request to the Documentero API and handled the response to retrieve the download link for the generated document.