Forum


Replies: 2   Views: 1692
Best practice for generating multiple documents
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by bkidd  · 26-06-2020 - 13:54

I would like to give users a way to populate a template using a CSV/Excel file.  I realize I could ask them to create a block that starts at the beginning of the body and ends at the end of the body.  I would ideally not require that they add the block if it is understood the entire document should be cloned.

In searching the documentation for bulk processing, I found replaceList (which looks promising), replaceWordFragment, and cloneWordContent.  I am trying to determine the simplest way to do this.  I would like a single document at the end that has the content cloned and replaced based on the number of rows.  I'd like to ensure each cloned content has a page break between them.

Any suggestions on how I can accomplish this?  I'm handling the improting of the CSV/Excel as well as the column to placeholder mapping so I have all the data and just need to understand the best way to clone an entire document - I think MS Word mail merge works this way.

Thanks

Posted by admin  · 26-06-2020 - 18:56

Hello,

You can use the template methods to work with a template DOCX and generate more than one output from an external content loading the same template for each output:

// iterate a data source to generate the documents
for ... {
  $docx = new CreateDocxFromTemplate('template.docx');
  // use template API methods to replace contents
  (...)
  // save the DOCX
  $docx->createDocx($output);
}

as explained on:

https://www.phpdocx.com/documentation/practical/working-with-templates

https://www.phpdocx.com/documentation/practical/adding-content-to-a-template

Or use Bulk Processing (https://www.phpdocx.com/documentation/introduction/bulk-processing-documents-PHP) features available since phpdocx 10 in Premium licenses.

If you need to clone a whole DOCX you can use mergeDocx merging the same document as many times as needed: https://www.phpdocx.com/api-documentation/docxutilities/merge-Word-documents-with-PHP . This method includes an option to force a section page break (enforceSectionPageBreak), or you can add a page break using DOCXPath if needed with insertWordFragment (https://www.phpdocx.com/api-documentation/docx-path/insert-elements-in-docx).

Regards.

Posted by bkidd  · 30-06-2020 - 20:04

Thanks, the bulk processing works great.