Forum


Replies: 5   Views: 3016
Merging documents at same time as another mixes output
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 admin  · 15-06-2017 - 19:01

Hello,

Since phpdocx 6.5, the library doesn't create temp files (except when generating charts, that create unique temp files for charts).

We think the problem may come from the names you are using to generate the DOCX to be merged. If you use the same file name for the DOCX to be merged or the merge DOCX outputs, then a race condition may happen. We recommend using unique file names to avoid this issue:

$fileName1 = uniqid('document', true) . 'docx';
$docx->createDocx($fileName1);

array_push($doc_arr, $fileName1)

Other solution, as you are using a Premium license, is to do in-memory merging to avoid creating DOCX files and get a better performance. This sample is included in phpdocx 7 (examples/DocxUtilities/mergeDocx/sample2_php file):

<?php

require_once 'classes/MultiMerge.inc';

CreateDocx::$returnDocxStructure = true;

// create the first document to be merged and return it as DOCX structure
$docx_a = new CreateDocx();

$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' .
    'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ' .
    'enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut' .
    'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit ' .
    'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' .
    'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui ' .
    'officia deserunt mollit anim id est laborum.';

$paragraphOptions = array(
    'bold' => true,
    'font' => 'Arial'
);

$docx_a->addText($text, $paragraphOptions);

$document1 = $docx_a->createDocx();

// create the second document to be merged and return it as DOCX structure
$docx_b = new CreateDocx();

$text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, ' .
    'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ' .
    'enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut' .
    'aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit ' .
    'in voluptate velit esse cillum dolore eu fugiat nulla pariatur. ' .
    'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui ' .
    'officia deserunt mollit anim id est laborum.';

$paragraphOptions = array(
    'font' => 'Arial'
);

$docx_b->addText($text, $paragraphOptions);

$footnote = new WordFragment($docx_b, 'document');
$footnote->addFootnote(
  array(
    'textDocument' => 'footnote',
    'textFootnote' => 'The footnote we want to insert.',
    'referenceMark' => array('b' => 'on'),
  )
);

$textFragment = new WordFragment($docx_b, 'document');

$text = array();
$text[] = array('text' => 'Other text ');
$text[] = $footnote;

$paragraphOptions = array(
  'textAlign' => 'center',
  'bold' => true,
);
$textFragment->addText($text, $paragraphOptions);

$htmlFragment = new WordFragment($docx_b, 'document');

$htmlFragmentString = new WordFragment($docx_b, 'document');
$htmlFragmentString->embedHtml('<p style="font-family: verdana; font-size: 11px">HTML tags <b>bold</b></p>');

$textHtml = array();
$textHtml[] = $htmlFragmentString;

$htmlFragment->addText($textHtml);

$valuesTable = array(
  array(
    $textFragment,
  ),
  array(
    '2',
  ),
  array(
    $htmlFragment,
  ),
);

$docx_b->addTable($valuesTable);

$document2 = $docx_b->createDocx();

CreateDocx::$returnDocxStructure = false;

$merge = new MultiMerge();
$merge->mergeDocx($document1, array($document2), 'example_merge_docx_2.docx', array());

Please use an unique file name as DOCX output when calling mergeDocx. If you need to return the merged document as DOCX stream instead of generating the file, you can use this option:

https://www.phpdocx.com/api-documentation/performance/zip-stream-docx-with-PHP

You can use the in-memory features with DOCX created from scratch and from templates.

Regards.