Forum


Replies: 13   Views: 1695
How to use phpdocx_section?
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 rapidtech  · 20-11-2019 - 13:16

I didn't find any explanation of how to use this tag. I have the next situation. The page should have a two-column layout but not necessarily the whole page. The beginning could be fullwidth text, then two columns and fullwidth text again

Posted by admin  · 20-11-2019 - 15:21

Hello,

phpdocx_section is included as a sample in Core/embedHTML/sample_6.php. This tag, as others from HTML Extended, works in the same way, this is using the options of the related method (addSection in this case).

MS Word allows generating sections on the same page using the continuous option. This is a simple sample (we recommend creating a single section for each embedHTML calling to simplify its use):

$html = '<p>Text</p>';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numberCols="3" />
<p>More text</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numberCols="1" />
<p>More text</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Regards.

Posted by rapidtech  · 21-11-2019 - 15:35

 In my case, I have a full HTML page code as following

$html = '

<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>

<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numberCols="3" />

<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>

<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numberCols="1" />

<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>

';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Is it possible to use it this way?

Posted by admin  · 21-11-2019 - 19:02

Hello,

You only can set a single phpdocx_section for each embedHTML call, so we recommend you to split the HTML content to be added.

Regards.

Posted by rapidtech  · 22-11-2019 - 10:51

Ok, I've managed to do this. Another issue is with converting the resulting DOCX to PDF. In the PDF doc each section goes to a new page.

$docx = new CreateDocx();

$html = '<p>1 Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="3" />
<p>2 Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="1" />
<p>3 Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$docx->createDocx('test.docx');

$transform = new TransformDocAdvPDF('test.docx');
$transform->transform('test.pdf');

How to get the exact same look in PDF as it is in the DOCX?

Posted by admin  · 22-11-2019 - 12:47

Hello,

continuous sections aren't supported in the native method conversion plugin yet. On http://Supported OOXML tags and attributes when using the native method (Supported OOXML tags and attributes when using the native method section) are detailed the supported contents and styles by this native transformation.

We recommend you to use the conversion plugin based on LibreOffice. On https://www.phpdocx.com/documentation/conversion-plugin you can read all information about installing it.

Regards.

Posted by rapidtech  · 25-11-2019 - 10:16

Ok. Thank you.

How to add a footer on each page? In my case, I am using sections as I've mentioned above. When I add a footer at the end of the code then the footer appears only on the last page. If I place the addFooter function in the very beginning, then the footer appears at each page but at the same time each section also starts from a new page.

$default = new WordFragment($docx, 'default');
$default->addText('default page footer.');
$docx->addFooter(array('default' => $default));

 

Posted by rapidtech  · 25-11-2019 - 13:58

As I understood, this is useful for creating different footers/headers. In my case, I need to have the same footer on each page. Everything works fine with phpdocx_section and multi-columns template till the moment I add the footer. Please, try the code below first without adding a footer and then with. You will have a totally different output.

$docx = new CreateDocx();

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="2" />
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text</p>';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="3" />
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="1" />
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));


/* Try with and witout this fragment */
$footer = new WordFragment($docx, 'defaultFooter');
$footer->addText('Footer text');
$docx->addFooter(array('default' => $footer));



$docx->createDocx('output');

 

Posted by admin  · 25-11-2019 - 14:39

Hello,

The documentation available on that page explains how to get different headers and footers when a DOCX has more than one section.

As you are adding more than one section to your DOCX, you need to add the footer/header before creating new sections, so the same header/footer will be used for all sections:

$docx = new CreateDocx();

$footer = new WordFragment($docx, 'defaultFooter');
$footer->addText('Footer text');
$docx->addFooter(array('default' => $footer));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="2" />
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text</p>';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="3" />
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text</p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$html = '
<phpdocx_section data-sectionType="continuous" data-paperType="A4" data-numbercols="1" />
<p>Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text </p>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

Regards.

Posted by rapidtech  · 25-11-2019 - 14:53

I did that before but it also works not the way I need. Yes, the footer will be on each page but then the sections work wrong. Please, try to generate the DOCX without a footer and you will see that it will be a one-page document with three sections following one by one. After adding a footer you will get three pages. And btw the footer text will appear twice at the first page.

Posted by admin  · 25-11-2019 - 15:06

Hello,

We have tested the script before posting it. After moving the addFooter code before the code that creates new sections, a single page is created with the new sections (two columns, three columns, and a single column) and a single footer. We have opened the DOCX output with MS Word 2010, MS Word 2013, MS Word 2016 and LibreOffice 6 and in all cases the content is correct.

We recommend you to check the code you are running, maybe some lines are duplicated or other issue. We have checked that our previous code is working correctly.

Regards.

Posted by rapidtech  · 25-11-2019 - 15:14

Yes, you are right. I've opened with the native macOS Pages and it looks incorrect. 

Thank you

Posted by admin  · 25-11-2019 - 15:19

Hello,

DOCX support in Pages is very limited and doesn't support many contents and styles.

Regards.