Forum


Replies: 5   Views: 444
How to insert a page break after every cloned blocked
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  · 06-09-2023 - 14:09

Hello,

You can use any of the following approaches:

  • Add the page break in the DOCX template manually (using MS Word, LibreOffice or any program you have used to create the document). The block placeholders must wrap the contents including the page break. In this case you can run:
$variables = array(
    array(
        'VAR_NAME' => 'John Doe',
        'VAR_DATE' => '11/08/2021',
    ),
    array(
        'VAR_NAME' => 'Jane Doe',
        'VAR_DATE' => '21/08/2021',
    ),
);

$docx->cloneBlock('FINDINGS', 1, $variables, array('removeBlockPlaceholder' => true));

$docx->deleteTemplateBlock('FINDINGS');

The block contens including the page break will be cloned.

  • Add a new placeholder at last block content that will be used to insert the page break (for example $VAR_BREAK$):
$BLOCK_FINDINGS$

$VAR_NAME$

$VAR_DATE$

$VAR_BREAK$

$BLOCK_FINDINGS$

In this case you need to use a WordFragment to add the page break in cloneBlock:

$breakFragment = new WordFragment($docx);
$breakFragment->addBreak(array('type' => 'page'));

$variables = array(
    array(
        'VAR_NAME' => 'John Doe',
        'VAR_DATE' => '11/08/2021',
        'VAR_BREAK' => $breakFragment,
    ),
    array(
        'VAR_NAME' => 'Jane Doe',
        'VAR_DATE' => '21/08/2021',
        'VAR_BREAK' => $breakFragment,
    ),
);

$docx->cloneBlock('FINDINGS', 1, $variables, array('removeBlockPlaceholder' => true));

$docx->deleteTemplateBlock('FINDINGS');
  • Use the firstMatch option to do block placeholder replacements instead of removing them. This approach is more complex but it's more flexible and can be useful if you can't change the template you are using to add the manual page break or the new placeholder.
$variables = array(
    array(
        'VAR_NAME' => 'John Doe',
        'VAR_DATE' => '11/08/2021',
    ),
    array(
        'VAR_NAME' => 'Jane Doe',
        'VAR_DATE' => '21/08/2021',
    ),
);

$docx->cloneBlock('FINDINGS', 1, $variables);

// rename and remove inital block placeholders
$docx->replaceVariableByText(array('BLOCK_FINDINGS' => '$BLOCK_FINDINGS_0$'), array('firstMatch' => true));
$docx->replaceVariableByText(array('VAR_NAME' => '$VAR_NAME_0$'), array('firstMatch' => true));
$docx->replaceVariableByText(array('VAR_DATE' => '$VAR_DATE_0$'), array('firstMatch' => true));
$docx->replaceVariableByText(array('BLOCK_FINDINGS' => '$BLOCK_FINDINGS_0$'), array('firstMatch' => true));
$docx->removeTemplateVariable('BLOCK_FINDINGS_0');
$docx->removeTemplateVariable('VAR_NAME_0');
$docx->removeTemplateVariable('VAR_DATE_0');
$docx->removeTemplateVariable('BLOCK_FINDINGS_0');

// remove the new first match of BLOCK_FINDINGS (this is the placeholder as the beginning of the block)
$docx->replaceVariableByText(array('BLOCK_FINDINGS' => '$BLOCK_FINDINGS_0$'), array('firstMatch' => true));
$docx->removeTemplateVariable('BLOCK_FINDINGS_0');

$breakFragment = new WordFragment($docx);
$breakFragment->addBreak(array('type' => 'page'));

// replace the new first match of BLOCK_FINDINGS (this is the placeholder as the end of the block)
$docx->replaceVariableByWordFragment(array('BLOCK_FINDINGS' => $breakFragment), array('firstMatch' => true));

// remove the new first match of BLOCK_FINDINGS (this is the placeholder as the beginning of the block)
$docx->replaceVariableByText(array('BLOCK_FINDINGS' => '$BLOCK_FINDINGS_0$'), array('firstMatch' => true));
$docx->removeTemplateVariable('BLOCK_FINDINGS_0');

// replace the new first match of BLOCK_FINDINGS (this is the placeholder as the end of the block)
$docx->replaceVariableByWordFragment(array('BLOCK_FINDINGS' => $breakFragment), array('firstMatch' => true));

Regards.