Forum


Replies: 7   Views: 434
Add pagination and contents to the footers replacing them in an existing docx
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 Diplodoc071  · 23-12-2021 - 13:40

The task is as follows:
Add a new page with html to an existing document.
Add pagination and text with an image (html) to the footers.

For some documents this works, for some it does not (footer only in last page).
Please tell me what is the reason and how to solve it.

Additionally required:
Transform to pdf, add background image.

My code:

$phpdocx=new CreateDocxFromTemplate(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$document_source);
$phpdocx->addBreak(array('type'=>'page'));
$phpdocx->embedHTML($new_page);
$numbering=new WordFragment($phpdocx,'defaultFooter');
$numbering->addPageNumber('numerical',array(
        'textAlign'=>'right',
        'bold'=>true,
        'sz'=>14,
        'color'=>'65a9ef',
));
$numbering->embedHTML($footer_html);
$phpdocx->addFooter(array('default'=>$numbering));
$phpdocx->createDocx(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name);
$phpdocx->transformDocument(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name);
$pdf_util=new PdfUtilities();
$pdf_util->addBackgroundImage(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.IMAGES_SETTINGS_DIR.$settings['wtm'],array('height'=>'auto','width'=>'auto','opacity'=>0.1));

 

Posted by admin  · 23-12-2021 - 15:11

Hello,

addHeader and addFooter methods only work with a single section. For this reason, the mergeDocx method available in Advanced and Premium licenses is needed to generate documents with multiple sections with different headers and footers (https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections).

If you need to overwrite headers or footers in a DOCX that contains one section, please note you may need to set all scopes (not only the default scope):

$docx->addFooter(array('default' => $numbering, 'first' => $numbering, 'even' => $numbering));

If you need to overwrite headers or footers in a DOCX that contains one or more sections, you can use the importHeadersAndFooters method (importing a DOCX that also includes all scopes of the headers/footers to be added: default, first, even):

$docx->importHeadersAndFooters('docx_footer.docx');

Regards.

Posted by Diplodoc071  · 24-12-2021 - 09:10

The problem occurs on documents that initially contain more than one section (section breaks «^b»). Is it possible to remove section breaks?
This code did not solve the problem:

$phpdocx->removeWordContent(array('type'=>'break'));
$content=new WordFragment($phpdocx,'document');
$phpdocx->replaceWordContent($content,array('type'=>'break'));

 

Posted by Diplodoc071  · 24-12-2021 - 09:23

All my code now:

$phpdocx=new CreateDocxFromTemplate(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$document_source);
$phpdocx->importHeadersAndFooters(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$document_source);
$phpdocx->removeWordContent(array('type'=>'break'));
$content=new WordFragment($phpdocx,'document');
$content->addText('');
$phpdocx->replaceWordContent($content,array('type'=>'break'));
$phpdocx->removeHeaders();
$phpdocx->removeFooters();
$phpdocx->addBreak(array('type'=>'page'));
$phpdocx->embedHTML($add_text);
$numbering=new WordFragment($phpdocx,'defaultFooter');
$numbering->addPageNumber('numerical',array(
        'textAlign'=>'right',
        'bold'=>true,
        'sz'=>14,
        'color'=>'65a9ef',
));
$numbering->embedHTML($footer_html);
$phpdocx->addFooter(array('default'=>$numbering,'first'=>$numbering,'even'=>$numbering));
$phpdocx->createDocx(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name);
$phpdocx->transformDocument(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name);
$pdf_util=new PdfUtilities();
$pdf_util->addBackgroundImage(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.IMAGES_SETTINGS_DIR.$settings['wtm'],array('height'=>'auto','width'=>'auto','opacity'=>0.1));

 

Posted by admin  · 24-12-2021 - 11:10

Hello,

As explained in our previous reply, addFooter and addHeader methods work only with documents with one section (to generate DOCX documents from scratch with multiple sections and headers/footers the mergeDocx method must be used [https://www.phpdocx.com/documentation/cookbook/headers-and-footers-for-sections]).

To replace all headers and footers in an existing DOCX document with one or more sections you need to use the importHeadersAndFooters method.

You can generate a DOCX with headers/footers:

$docx = new CreateDocx();

$footer_html = '<p>Footer HTML</p>';

$numbering = new WordFragment($docx, 'defaultFooter');
$numbering->addPageNumber('numerical',array(
        'textAlign'=>'right',
        'bold'=>true,
        'sz'=>14,
        'color'=>'65a9ef',
));
$numbering->embedHTML($footer_html);
$docx->addFooter(array('default' => $numbering, 'first' => $numbering, 'even' => $numbering));

$docx->createDocx('docx_footers');

and then use it with importHeadersAndFooters to replace headers/footers in a DOCX template with one or more sections:

$docxNew = new CreateDocxFromTemplate('document_with_header_footer_multiple_sections.docx');
$docx->addBreak(array('type'=>'page'));
$docx->embedHTML($add_text);
$docx->importHeadersAndFooters('docx_footers.docx');
$docx->createDocx('new_document.docx');

Also note that break (addBreak method and 'type'=>'break' in DOCXPath) don't work with sections but with break tags. If you need to work with sections you need to use the addSection method and 'type' => 'section' in DOCXPath. The removeWordContent method included in DOCXPath removes contents; in the package you can find a lot of samples using it.

If you open a ticket (https://www.phpdocx.com/support) attaching a DOCX sample with multiple sections you are using, the dev team will generate a custom script using it.

Regards.

Posted by Diplodoc071  · 12-01-2022 - 15:02

Thanks a lot, this solved the problem.
But now the document loses its previous headers and footers.
Can you please tell me if it is possible to combine the new headers and footers with the old headers and footers from the template?

$phpdocx=new CreateDocxFromTemplate(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$document_source);
$phpdocx_numbering=new CreateDocx();
$numbering=new WordFragment($phpdocx_numbering,'defaultFooter');
$numbering->addPageNumber('numerical',array(
        'textAlign'=>'right',
        'bold'=>false,
        'sz'=>12,
        'color'=>'65a9ef',
        'spacingBottom'=>0,
        'spacingTop'=>0,
));
$numbering->addText($numbering_text,array(
        'textAlign'=>'right',
        'bold'=>false,
        'sz'=>8,
        'color'=>'65a9ef',
));
$phpdocx_numbering->addFooter(array('default'=>$numbering,'first'=>$numbering,'even'=>$numbering));
$phpdocx_numbering->createDocx(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$numbering_name);
$phpdocx->addBreak(array('type'=>'page'));
$phpdocx->embedHTML($add_text);
$phpdocx->importHeadersAndFooters(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$numbering_name);
$phpdocx->createDocx(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name);
$phpdocx->transformDocument(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name);
$pdf_util=new PdfUtilities();
$pdf_util->addBackgroundImage(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.IMAGES_SETTINGS_DIR.$settings['wtm'],array('height'=>'auto','width'=>'auto','opacity'=>0.1));

 

Posted by admin  · 12-01-2022 - 17:22

Hello,

importHeadersAndFooters remove existing headers/footers to add the imported ones. There's no method to add new headers and footers keeping existing ones in a DOCX document. A custom development should be done for this specific task.

Using template methods you can replace placeholders in headers and footers (and also other targets: document, footnotes, comments...). Using DOCXPath you can add, remove and replace contents in the main document, headers and footers; but headers and footers must exist to be able to change them.

Regards.

Posted by Diplodoc071  · 13-01-2022 - 12:19

Got it, thanks!
We had to stop changing headers and footers in the docx and add them directly to pdf.
This solves our problem.

$phpdocx=new CreateDocxFromTemplate(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$document_source);
$phpdocx->addBreak(array('type'=>'page'));
$phpdocx->embedHTML($add_text);
/*
$phpdocx_numbering=new CreateDocx();
$numbering=new WordFragment($phpdocx_numbering,'defaultFooter');
$numbering->addPageNumber('numerical',array(
        'textAlign'=>'right',
        'bold'=>false,
        'sz'=>12,
        'color'=>'65a9ef',
        'spacingBottom'=>0,
        'spacingTop'=>0,
));
$numbering->addText($numbering_text,array(
        'textAlign'=>'right',
        'bold'=>false,
        'sz'=>8,
        'color'=>'65a9ef',
));
$phpdocx_numbering->addFooter(array('default'=>$numbering,'first'=>$numbering,'even'=>$numbering));
$phpdocx_numbering->createDocx(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$numbering_name);
$phpdocx->importHeadersAndFooters(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$numbering_name);
*/
$phpdocx->createDocx(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name);
$phpdocx->transformDocument(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$temp_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name);
$pdf_util=new PdfUtilities();
$pdf_util->addBackgroundImage(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,ROOT_DIR.IMAGES_SETTINGS_DIR.$settings['wtm'],array('height'=>'auto','width'=>'auto','opacity'=>0.1));
class D_TCPDI extends TCPDI{
        function Header(){
        }
        function Footer(){
                $this->SetY(-10);
                $this->SetFont('dejavusans','',8);
                $this->SetTextColor(101,169,239);
                $this->Cell(0,0,'Page '.$this->PageNo().' of '.$this->count_pages,0,0,'R');
                $this->Cell(0,10,$this->numbering_text,0,0,'R');
        }
}
$dtcpdi=new D_TCPDI(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,'UTF-8',false);
$dtcpdi_pages=$dtcpdi->setSourceFile(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name);
$dtcpdi->numbering_text=$numbering_text;
$dtcpdi->count_pages=$dtcpdi_pages;
for($i=1;$i<=$dtcpdi_pages;$i++){
        $dtcpdi_page=$dtcpdi->importPage($i);
        $dtcpdi->AddPage();
        $dtcpdi->useTemplate($dtcpdi_page);
}
$dtcpdi->Output(ROOT_DIR.UPLOAD_DOCUMENTS_DIR.$pdf_name,'F');