Forum


Replies: 13   Views: 3233
Add section and change page orientation with htmlextended
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  · 24-05-2019 - 07:24

Hello,

An alternative code to generate custom sections in the same embedHTML is using WordFragments with raw WordML example, for example the following code generate two section types and add them into the workflow:

$section1Fragment = new Phpdocx\Elements\WordFragment($docx);
$section1Fragment->addWordML('<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pPr><w:sectPr><w:pgSz w:code="9" w:h="16838" w:orient="portrait" w:w="11906"/><w:pgMar w:bottom="850" w:footer="708" w:gutter="0" w:header="708" w:left="1077" w:right="1077" w:top="850"/><w:cols w:num="1" w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr></w:pPr></w:p>');
$section2Fragment = new Phpdocx\Elements\WordFragment($docx);
$section2Fragment->addWordML('<w:p xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:pPr><w:sectPr><w:type w:val="nextPage"/><w:pgSz w:code="9" w:h="11906" w:orient="landscape" w:w="16838"/><w:pgMar w:bottom="850" w:footer="708" w:gutter="0" w:header="708" w:left="1077" w:right="1077" w:top="850"/><w:cols w:num="1" w:space="708"/><w:docGrid w:linePitch="360"/></w:sectPr></w:pPr></w:p>');

$html = '
    <phpdocx_modifypagelayout data-paperType="A4" data-marginTop="850" data-marginRight="1077" data-marginBottom="850" data-marginLeft="1077" />
    <p>Paragraph 1</p>
    <p>Paragraph 2</p>

    <phpdocx_wordfragment data-content="'.base64_encode(serialize($section1Fragment)).'" />
    <p>Paragraph A</p>
    <p>Paragraph B</p>

    <phpdocx_wordfragment data-content="'.base64_encode(serialize($section2Fragment)).'" />
    <p>Paragraph I</p>
    <p>Paragraph II</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Following this approach, please note you need to generate sections as raw WordML contents setting margins, orientation, cols and other values and then add them as WordFragments into the HTML. For example you could generate two (or more) raw WordML fragments: A4, A4-landscape with your custom margins, and then use them as needed (one or multiple times):

$sectionA4Fragment = new Phpdocx\Elements\WordFragment($docx);
$sectionA4Fragment->addWordML('...');
$sectionA4LandscapeFragment = new Phpdocx\Elements\WordFragment($docx);
$sectionA4LandscapeFragment->addWordML('...');

$html = '
    <phpdocx_modifypagelayout data-paperType="A4" data-marginTop="850" data-marginRight="1077" data-marginBottom="850" data-marginLeft="1077" />
    <p>Paragraph</p>
    <p>Paragraph</p>

    <phpdocx_wordfragment data-content="'.base64_encode(serialize($sectionA4Fragment)).'" />
    <p>Paragraph</p>
    <p>Paragraph</p>

    <phpdocx_wordfragment data-content="'.base64_encode(serialize($sectionA4LandscapeFragment)).'" />
    <p>Paragraph</p>
    <p>Paragraph</p>

    <phpdocx_wordfragment data-content="'.base64_encode(serialize($sectionA4Fragment)).'" />
    <p>Paragraph</p>
    <p>Paragraph</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Regards.