Do you know any way to generate doc
and docx
files with PHP and without COM component? I've tried PHPWord
, which creates docx files, but these cannot be opened in OpenOffice
because they cause it to crash. I've also tried PHPDocx
, but it didn't generate any files at all.
Generating word documents with JS:
I have created a simple open-source library that will replace tags by values.
For example Hi {name}
with data={name:"John"}
will be replaced by Hi John
.
Here it is : https://github.com/edi9999/docxtemplater
Demo: https://docxtemplater.com/demo
See here:
http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
To quote from the article the most common method:
In this method you need to format the HTML/PHP page using Word-friendly CSS and add header information to your PHP script. Make sure you don't use external style sheets since everything should be in the same file.
As a result user will be prompted to download a file. This file will not be 100% "original" Word document, but it certainly will open in MS Word application. You can use this method both for Unix and Windows environments.
<?php
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");
echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With