Add contents to different document types
The flexibility of phpdocx allows to work very easily with any type of DOCX. That includes adding contents to documents created from scratch or to existing documents as well as modifying the contents of documents and templates.
phpdocx incorporates a base template to create new documents from scratch, that is, without any preexisting content.
With the different methods available in the phpdocx API it is possible to add contents. To do this it is necessary to instantiate the CreateDocx class.
For example, to generate a new DOCX with a text paragraph, type the following code:
The base template is a DOCX file that phpdocx reads to create the documents. Premium licenses let you delete the accesses to disk through the upload of this base template stored in memory.
To replace contents in an existing DOCX you have to instantiate the class CreateDocxFromTemplate, indicating the document that would serve as a template.
For example, to open the file template.docx and substitute the placeholder VAR for the text PHPDOCX, use the following code:
The templates are DOCX that exist in the file system that phpdocx reads to generate documents. Premium licenses let you delete the accesses to disk through the upload of this base template stored in memory.
From scratch using base templates
Another available method that allows to work with the documents is to choose a preexisting DOCX as a base template. You can add to that template contents after its original elements.
The code that follows defines the document my_document.docx as a base template. We will add a new text after the original content:
The constant PHPDOCX_BASE_TEMPLATE is required in order to manage the default styles of phpdocx.
You can either add or replace the contents in a DOCX document by instantiating the class CreateDocxFromTemplate.