Uncategorized

How to generate a word docx from a template with PHP ?

18

Hey folks,

A lot of clients of mine asked for docx generation in the past.

The only solution that I knew of in PHP is the SOAP client phplivedocx.

first of all It was kinda slow and I didn’t want to relly on a service and more importantly It was missing the “nested block” feature.

So I decided to create my own class !

Have fun & fork me if you need another feature !

http://github.com/djpate/docxgen

DOCXGEN

Features

  • Create valid docx based on a template
  • Nested blocks on infinite levels

How to create your template

Simply open up word 2007+

If you want to map a single field you can just use #NAME# but you could use anything you like since it’s just a search & replace

To create a block

[start blockname] your content [start somenestedblock] [end somenestedblock] [end blockname] 
  • blockname should be unique
  • blockname has to match w+ reg ex (a-zA-Z_)
  • fields mapped in block has to be unique

Please check out full template in the repo

How to setup

require("phpDocx.php"); $phpdocx = new phpdocx("mytemplate.docx"); 

How to assign values

$phpdocx->assign("#TITLE1#","Hello !"); // basic field mapping $phpdocx->assignBlock("members",array(array("#NAME#"=>"John","#SURNAME#"=>"DOE"),array("#NAME#"=>"Jane","#SURNAME#"=>"DOE"))); // this would replicate two members block with the associated values $phpdocx->assignNestedBlock("pets",array("#PETNAME#"=>"Rex"),array("members"=>1)); // would create a block pets for john doe with the name rex $phpdocx->assignNestedBlock("pets",array("#PETNAME#"=>"Rox"),array("members"=>2)); // would create a block pets for jane doe with the name rox $phpdocx->assignNestedBlock("toys",array("#TOYNAME#"=>"Ball"),array("members"=>1,"pets"=>1)); // would create a block toy for rex $phpdocx->assignNestedBlock("toys",array("#TOYNAME#"=>"Frisbee"),array("members"=>2,"pets"=>1)); // would create a block toy for rox 

How to save

$phpdocx->save("somefile.docx"); 

More info

Why this pclzip ?

I’m using pclzip for the zip process because the zip utility provided with php can cause issue with office

What’s the licence ?

GPL

Anything else ?

I’m using three function from the TBS library so congrats to them

Share

Related Posts:

Dealing with Monster.com SOAP with PHP5

0

Recently I had to set up a system that would post a job offer to monster.com web service using SOAP.

Once I made the script to generate the XML I started looking into the SOAP class that PHP5 provides but I soon started to feel the headache comming !

So I justed wanted to help out If some of you were facing the same issue : FORGET that soap class and use a basic CURL !

Here is my code snippet

$headers=array("Accept: text/xml","Content-Type: text/xml");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"https://208.71.198.74:8443/bgwBroker");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml); // your soap xml that you generated earlier
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
$return = curl_exec($curl);
curl_close ($curl);

And there you go ! you just have to handle the return info.

I hope that this will help someone one day !

Share

Related Posts:

Go to Top