php docx template modification

20.4k views Asked by At

Has anybody had any experience editing a docx template. My php admin area needs the functionality to modify docx templates, this functionality will be used a lot so memory is important.

I have found phpword which is in beta, it works but not 100%

I have been googling and found phpdocx, has anybody used this and can give me some feedback?

Are there any other solutions all i need is the ability to change text and maybe an image within a docx template.

I will be prepared to pay for a service but not masses and a one off fee for license would be preferred.

4

There are 4 answers

7
Skrol29 On BEST ANSWER

You should try OpenTBS.

It's an open source PHP library which builds DOCX with the technique of templates.

No temp directory, no extra exe needed. First create your DOCX, XLSX, PPTX with Ms Office, (ODT, ODS, ODP are also supported, that's OpenOffice files). Then you use OpenTBS to load the template and change the content using the Template Engine (easy, see the demo). At the end, you save the result where you need. It can be a new file, a download flow, a PHP binary string.

OpenTBS can also change pictures and charts in a document.

0
Camix On

Phpdocx suits your needs. Use a very easy template format based on $variable$ on a docx documents so it's very easy works with the library. I'm using for several months and for me is a good tool.

2
user8057921 On
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Employee Details</title>
  </head>
  <body>
    <form method="post" action="#">
      <input type="text" name="e_name" />        
      <input type="text" name="e_email" />
      <input type="submit" name="e_submit" />
    </form>
  </body>
  <?php
    if(isset($_POST["e_submit"]))
    {
        $name=(string) $_POST["e_name"];
        $email=(string) $_POST["e_email"];

        $source='template1.docx';
        $destination='template_'.$name.'.docx';
        $temp='template_'.$name.'.docx';

        copy($source,$temp);

        $zip=new ZipArchive;

        $fileXml='word/document.xml';
        if($zip->open($temp) === TRUE)
        {
            $old=$zip->getFromName($fileXml);

            $new=str_replace('{{Name}}',$name,$old);
            $new=str_replace('{{Email}}',$email,$new);

            $zip->deleteName($fileXml);
            $zip->addFromString($fileXml,$new);
            $zip->close();

            header("Content-Type: application/force-download");
            header('Content-Type: application/msword');
            header('Content-Disposition: attachment; filename="'.$destination.'"');

            readfile($destination);
             unlink($destination);
           exit();
        }
    }
  ?>
</html>
2
Sergey Shuchkin On

PHP DOCX Template

http://www.phpclasses.org/package/8247-PHP-Create-Microsoft-Word-documents-from-templates.html

include( 'docxtemplate.class.php' );

$docx = new DOCXTemplate( 'invoice.tpl.php' );

$docx->set(array(
    'org' => 'MyCompany LLC',
    'addr' => 'Green Street, 82'
));

$docx->downloadAs('invoice.docx');

EDIT: Fixed correct method name downloadAs() instead of download()