3

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.

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
Blu Towers
  • 1,908
  • 4
  • 17
  • 20

4 Answers4

11

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.

Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • I was just about to come back saying i found this what do you guys think. The site could do with a redesign to make reading the documentation easier but i think i have finally found what i am after, thanks. – Blu Towers Oct 25 '11 at 08:22
  • 1
    OpenTbs does what i need but i have hit a snag, how would i replace images in the template that have certain names, picture1.jpg,picture2.jpg ect without having to insert a []tag after each of them. I have searched and this post touches on it but a solution is found only for open office doc types. http://stackoverflow.com/questions/1780130/generate-odt-documents-with-dynamic-images-in-php – Blu Towers Oct 25 '11 at 10:06
  • Hi, using a [] tag in the document enables you to change the image without any technical consideration. Not to mention that a picture has no name or no visible id when you use Ms Office. OpenTBS let you directly add or delete an inner files in the Docx. But not yet for changing an existing inner file. – Skrol29 Oct 25 '11 at 15:54
  • so when you add the picture into ms word you lose the filename? so picture1.jpg will not be called picture1.jpg in the docx zip file? – Blu Towers Oct 25 '11 at 15:57
  • That is correct. Pictures are renamed inside. – Skrol29 Oct 26 '11 at 00:33
  • changing the picture is a bit annoying any ideas how i can make it remove the picture if none found e.g. default=current, i tried leaving that out but it didnt delete the pic. – Blu Towers Oct 26 '11 at 10:48
  • still no joy on deleting the picture if the one your trying to replace it with does not exist...really annoying would have thought it would be a feature – Blu Towers Oct 28 '11 at 12:40
2
<!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>
Abhishek
  • 2,925
  • 4
  • 34
  • 59
  • Please explain your answer. – zuluk May 24 '17 at 07:42
  • zuluk... Docx files are basically zip files. To understand, change the ".docx" to" .zip" and open it with your fav archiving program. What that user's script is trying to do is opening the part of the archive that contains the text. Then using PHP replace the delimiters with what you really want there dynamically then archive again as the propagated .docx file – JSG May 17 '21 at 03:19
1

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()

David
  • 369
  • 3
  • 14
Sergey Shuchkin
  • 2,037
  • 19
  • 9
0

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.

Camix
  • 1