Last Updated: 24 Jul, 2025
Working with PDFs in web applications has become a common requirement across industries. Whether you’re managing invoices, contracts, or academic content, being able to convert PDF documents to editable formats like Microsoft Word (DOCX) is essential. Fortunately, with the help of powerful and free PHP APIs, developers can automate and streamline this process with ease.
Why Convert PDF to Word in PHP?
PDF files are excellent for distribution because they preserve layout and design. However, editing them is not always straightforward. Word documents, on the other hand, are highly editable, making them ideal for tasks that require user interaction, updates, or collaboration. A PHP-based solution allows developers to:
- Enable server-side batch PDF-to-Word conversion
- Integrate document editing workflows in web apps
- Automate report generation and document processing
- Allow end-users to download editable versions of their files
Here are some of the most reliable and free PHP APIs and libraries that can help you perform PDF-to-Word conversion with minimal setup.
PHPWord with a PDF Parser
For more control over the conversion process, you can combine the power of PHPWord, a library for creating and manipulating Word documents, with a PDF parsing library.
Code Example Explanation:
This example first uses the PDFParser library to read the PDF and extract the text from each page. Then, it uses PHPWord to create a new Word document and add the extracted text to it. This method gives you fine-grained control over the output, allowing you to format the text and add other elements as needed.
require_once 'vendor/autoload.php';
// Create a new PDF parser object
$parser = new \Smalot\PdfParser\Parser();
// Parse the PDF file
$pdf = $parser->parseFile('path/to/your/document.pdf');
// Retrieve all pages from the PDF
$pages = $pdf->getPages();
// Create a new PHPWord object
$phpWord = new \PhpOffice\PhpWord\PhpWord();
// Add a section to the Word document
$section = $phpWord->addSection();
// Loop through each page of the PDF
foreach ($pages as $page) {
// Extract the text from the page
$text = $page->getText();
// Add the text to the Word document
$section->addText($text);
}
// Save the Word document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('output.docx');
echo "PDF content extracted and saved to Word successfully!";
Spatie PDF-to-Text + PHPWord (Text-based Conversion)
If your PDF files mainly contain plain text (not complex tables or images), you can combine the open source libraries Spatie PDF-to-Text and PHPWord for a lightweight conversion pipeline.
How It Works?
This is the most straightforward method if you have access to Adobe Acrobat Pro.
Steps:
- Frist you need to use Spatie\PdfToText API, which extracts plain text from PDFs.
- The PHPWord library is used to generate a Word document with the extracted content.
- Best suited for text-only PDFs without complicated formatting.
Here is a code example that shows how to extract text from a PDF file and then generate a Word documents with extracted contents using PHPWord library.
PdfReader reader = new PdfReader("form.pdf");
FileOutputStream out = new FileOutputStream("data.fdf");
FdfWriter writer = new FdfWriter();
writer.setField("name", "John Doe");
writer.writeTo(out);
reader.close();
out.close();
Gotenberg via PHP (Open Source Docker Microservice)
Gotenberg is an open source document conversion microservice that can be integrated with PHP using HTTP requests. It’s ideal for developers who prefer Docker-based tools.
How It Works?
- Gotenberg uses LibreOffice in the backend to convert PDFs.
- You send a POST request with the PDF file.
- The server returns the DOCX document.
Code Snippet (PHP + Gotenberg)
Aspose.PDF Cloud for PHP (Free Tier)
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:3000/forms/libreoffice/convert');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$data = [
[
'name' => 'files',
'contents' => fopen('input.pdf', 'r'),
'filename' => 'input.pdf'
]
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
file_put_contents('output.docx', $response);
curl_close($ch);
echo "Conversion done via Gotenberg!";
Aspose.PDF Cloud for PHP (Free Tier)
Aspose.PDF Cloud PHP SDK provides a powerful PHP SDK that allows software developers to convert PDF files to Word format directly using their cloud API. You can start for free with limited API calls. Here is a simple example that shows how software developers can load and convert a PDF file into Microsoft Word DOCX documents using PHP commands.
Code Example:
use Aspose\Pdf\Cloud\PdfApi;
$pdfApi = new PdfApi('YOUR_APP_KEY', 'YOUR_APP_SID');
$pdfApi->putConvertDocument('sample.pdf', ['format' => 'docx']);
Final Thoughts
Converting PDF files to Word documents using PHP doesn’t have to be complex or expensive. With the right free tools and libraries, developers can offer seamless, editable document exports to users, boost document automation, and improve overall productivity. Whether you prefer lightweight PHP wrappers or robust cloud-based APIs, there’s a solution to suit your needs.
FAQ
Q: Can I convert PDF to Word in PHP without using paid APIs?
A: Yes, you can use open source libraries like Spatie, PHPWord, and LibreOffice CLI for free.
Q: Does open source PDF to Word conversion support images and formatting?
A: Basic tools don’t, but using LibreOffice or Gotenberg preserves layout and images.
Q: Is it possible to automate PDF to DOCX conversion in PHP web apps?
A: Absolutely—these libraries can be integrated into any PHP-based workflow.