Posts tagged github

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

21

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:

jTag : A jQuery plugin to tag pictures & more !

57

Howdy,

A little while ago I published an article on how to create a facebook like tagging system.
Even thought It worked fine it was a bit messy and kinda hard to impletement.

I then started to work on a jQuery plugin that make that feature a lot easier to integrate on your solutions.

What does it look like ?

Click on the picture to start tagging

I made more demos to explain all the options of the script.

Where do I get it ?

You can get the source code on github or git clone git://github.com/djpate/jTag.git

(more…)

Share

Related Posts:

gmail, yahoo & live messenger contact inviter script

20

Howdy,

you guys have probably noticed that most of the contact inviter script out there were broken now because most of the new api’s only support Oauth protocol instead of the good ol’ login / password authorization.

It’s great because now you don’t have to give the those website your login information but It is a bit more complicated for us developers to set it up so I’ve decided to create a small set of classes that will allow you to easily implement such a feature on your website.

First off all, you can check out the simple proof of concept by checking out the demo here and getting the script on gitHub here.

Some people who may know my blog will notice that I am in no way a designer so It doesnt look amazing but it works and that’s what this article is about ! ( if someone feels like he could make it look better you can get the code from github. see at the end of the article for more info).

Ok so now the fun stuff.
(more…)

Share

Related Posts:

Go to Top