Forum


Replies: 3   Views: 176
Is it possible to inject the number of pages and page number with embedhtml
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 jake@dewittsolutions.com  · 12-01-2024 - 21:32

Hi I am wanting to embed the number of pages and current page number into the header (and maybe even the footer) of a document using the embedHTML(). PHPOffice has done something similar to this with thier {PAGE} and {NUMPAGES} constants. I am curious if the same thing can be accomplished with PHPDocx but adding it specifically within a block of HTML that I then embed into the header for example the code might look something like this:
 

$docx = new CreateDocx();
$headerHTML = '<div>Page {PAGE} of {NUMPAGES}</div>';
$headerFragment = new WordFragment($docx, 'defaultHeader');
$headerFragment->embedHTML($headerHTML);
$docx->addHeader(['default' => $headerFragment]);

$html = '<h1>Document Content</h1>'
$docx->embedHTML($html);
$docx->createDocx('path/to/file.docx');

Posted by admin  · 13-01-2024 - 07:38

Hello,

Using HTML Extended, available in Premium licenses, you can call phpdocx methods from HTML tags. For example, using phpdocx_pagenumber (https://www.phpdocx.com/htmlapi-documentation/html-extended/insert-page-number-Word-document-with-HTML) you can insert page numbering.

On https://www.phpdocx.com/documentation/htmlapi-documentation you can read all supported HTML tags. On https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP you can read more information about HTML Extended and CSS Extended.

Regards.

Posted by jake@dewittsolutions.com  · 13-01-2024 - 17:52

Great thank you for pointing me to this, and it is working properly in my tests. I have couple more questions on this, first would it be possible to echo out just the total number of pages separate from the current page number. For example say I wanted to just show the total number of pages or maybe change up the text to say it in a different way for example "Current Page: <phpdocx_pagenumber data-target="defaultHeader" data-type="numerical" /> Total Pages: <TotalPages/> ".

Also related to that is there a way to apply css styling to this custom html tag from a parent HTML element. For example take the previous example if I wanted to change the font color of all of that text ideally I could wrap all of that within a div or something and apply the styling to the div and have that apply to everything contained within it:

<div style="color: red;">
Current Page: <phpdocx_pagenumber data-target="defaultHeader" data-type="numerical" /> Total Pages: <TotalPages/>
</div>

Would something like this be possible?
 

Posted by admin  · 14-01-2024 - 08:40

Hello,

Please read the information available on https://www.phpdocx.com/documentation/snippets/translate-page-of-contents to change the text contents in page-of numbering. You need to set a new OOXMLResources::$pageNumber content.

Regarding the style, instead of CSS styles, in this case you need to set a custom paragraph style with the data-pStyle attribute (https://www.phpdocx.com/htmlapi-documentation/html-extended/insert-page-number-Word-document-with-HTML). This phpdocx_pagenumber tag calls the addPageNumber method (https://www.phpdocx.com/api-documentation/word-content/insert-page-number-Word-document-with-PHP), so the same options are available. In the case of the page-of type, a custom paragraph style must be applied to set custom styles.

Regards.