How to convert DOCX to PDF using PHP

This tutorial demonstrates how to convert DOCX files to PDF using Nutrient’s PHP API. The guide walks through creating a free API account, obtaining API credentials, and implementing a simple PHP script using cURL for document conversion. The implementation showcases how to use the API’s document conversion capabilities, which can be combined with other API tools for complex document processing workflows like merging, OCR, watermarking, and page manipulation.
In this post, you’ll learn how to convert DOCX files to PDFs using Nutrient’s DOCX-to-PDF PHP API. With our API, you receive 100 credits with the free plan. Different operations on a document consume different amounts of credits, so the number of PDF documents you can generate may vary. All you need to do is create a free account(opens in a new tab) to get access to your API key.
Nutrient API
Document conversion is just one of our 30+ PDF API tools. You can combine our conversion tool with other tools to create complex document processing workflows. You’ll be able to convert various file formats into PDFs and then:
- Merge several resulting PDFs into one
- OCR, watermark, or flatten PDFs
- Remove or duplicate specific PDF pages
Once you create your account, you’ll be able to access all our PDF API tools.
Step 1 — Creating a free account on Nutrient
Go to our website(opens in a new tab), where you’ll see the page below, prompting you to create your free account.
Once you’ve created your account, you’ll be welcomed by the page below, which shows an overview of your plan details.
As you can see in the bottom-left corner, you’ll start with 100 credits to process, and you’ll be able to access all our PDF API tools.
Step 2 — Obtaining the API key
After you’ve verified your email, you can get your API key from the dashboard. In the menu on the left, click API Keys. You’ll see the following page, which is an overview of your keys:
Copy the Live API Key, because you’ll need this for the DOCX-to-PDF API.
Step 3 — Setting up folders and files
Now, create a folder called docx_to_pdf
and open it in a code editor. For this tutorial, you’ll use VS Code as your primary code editor. Next, create two folders inside docx_to_pdf
and name them input_documents
and processed_documents
.
Next, copy your DOCX file to the input_documents
folder and rename it to document.docx
. You can use our demo document as an example.
Then, in the root folder, docx_to_pdf
, create a file called processor.php
. This is the file where you’ll keep your code.
Your folder structure will look like this:
docx_to_pdf├── input_documents| └── document.docx├── processed_documents└── processor.php
Step 4 — Writing the code
Open the processor.php
file and paste the code below into it:
<?php
$FileHandle = fopen('processed_documents/result.pdf', 'w+');
$curl = curl_init();
$instructions = '{ "parts": [ { "file": "document" } ]}';
curl_setopt_array($curl, array( CURLOPT_URL => 'https://api.nutrient.io/build', CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_POSTFIELDS => array( 'instructions' => $instructions, 'document' => new CURLFILE('input_documents/document.docx') ), CURLOPT_HTTPHEADER => array( 'Authorization: Bearer YOUR API KEY HERE' ), CURLOPT_FILE => $FileHandle,));
$response = curl_exec($curl);
curl_close($curl);
fclose($FileHandle);
Make sure to replace YOUR_API_KEY_HERE
with your API key.
Code explanation
In the code above, you create a FileHandle
variable that will allow you to save the file in the processed_documents
folder.
Then, you create the instructions
variable, where all the instructions for the API will be stored in the form of a JSON string. Finally, you make a CURL
request to process the target file.
Output
To execute the code, run the command below:
php processor.php
On successful execution, you’ll see a new processed file, result.pdf
, located in the processed_documents
folder.
The folder structure will look like this:
docx_to_pdf├── input_documents| └── document.docx├── processed_documents| └── result.pdf└── processor.php
Final words
In this post, you learned how to easily and seamlessly convert DOCX files to PDF documents for your PHP application using our DOCX-to-PDF PHP API.
You can integrate all of these functions into your existing applications. With the same API token, you can also perform other operations, such as merging several documents into a single PDF, adding watermarks, and more. To get started with a free trial, sign up(opens in a new tab) here.