Forum


Replies: 5   Views: 2700
How to place different qr-codes to pages in a docx-document?
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 alex_passionfive  · 06-05-2019 - 10:53

Hello all!

I generate a docx-document using a docx-template with defined footer, header and reserved place for a QR-code. I need to place to each page an unique QR-code. For example: on the page 1 the QR-code with the text "Page 1/12", on the page 2 - "Page 2/12" etc. How can I achieve it?

Thanks a lot.

Posted by admin  · 06-05-2019 - 11:09

Hello,

You need to use your template to generate as many DOCX documents as needed (for example one document for each page you want in the final DOCX) replacing the image/placeholder by the new QR using replacePlaceholderImage or replaceVariableByWordFragment. And then merge them into a single document using mergeDocx, the numbering option of this method allows continue and restart the numbering.

If the final DOCX has a placeholder that appear more than one time, and it needs to be replaced by unique contents, the firstMatch option available in replace methods can be used.

Regards.

Posted by alex_passionfive  · 06-05-2019 - 12:49

If the final DOCX has a placeholder that appear more than one time, and it needs to be replaced by unique contents, the firstMatch option available in replace methods can be used.

firstMatch doesn't work for me, if the placeholder is located in the footer or in the header. The QR-Code should be at the top of the document. Is there a solution? And if I set firstMatch=true, how can I replace the placeholder in the rest of the pages?

Posted by admin  · 06-05-2019 - 13:59

Hello,

The firstMatch option works correctly in headers and footers, but it replaces the first occurrence of the placeholder of each header and footer. 

Please note that in MS Word headers/footers work per section, not per page. So if the contents of a section fill more than one page the same header and footer is used. MS Word allows generating new headers and footers per section, not per page, but you can do page section breaks to generate new pages for new sections.

We recommend you to work with as many DOCX documents as needed with their own sections, do the needed changes with phpdocx and then merge the documents into a single DOCX using mergeDocx.

About the firstMatch question, it can be used iterating the values to be replaced. The first match is replaced and the placeholder removed, then the next one and so on.

Regards.

Posted by alex_passionfive  · 06-05-2019 - 14:17

Sorry. I don't understand, what you mean. I have a docx-template with the placeholder $body$. And $body$ is replaced with replaceVariableByHTML. I don't know, how many pages has the generated document. But I need on each page on the top a QR-code with (Page 1/12, Page 2/12 etc). The page number should stand In the footer of the generated document.

 

$docx = new \CreateDocxFromTemplate($filename);

$docx->replaceVariableByHTML(
        'body',
        'block',
        $html,
        array(
            'isFile' => false,
            'parseDivsAsPs' => true,
            'downloadImages' => false,
            'useHTMLExtended' => true,
        )
);

$docx = new \CreateDocxFromTemplate($tempDocxFileName);

 

Posted by admin  · 06-05-2019 - 14:34

Hello,

All pages of a section share the same headers and footers. It's not possible to add contents to a section that fills more than one page and then add one header for the first page, other for the second page and so on as you say you need to do in the topic: I need to place to each page an unique QR-code. DOCX documents don't allow this.

You need to work with sections to be able to have different headers and footers (https://www.techrepublic.com/blog/microsoft-office/accommodate-different-headers-and-footers-in-a-word-document/). The page numbering is a special content type can be continuous along the sections and it's generated automatically (from the template contents or using addPageNumber).

Regards.