Forum


Replies: 3   Views: 4122
Problems with variable replacement
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 campagnuolo  · 17-11-2015 - 15:58

Hello,
I'm actually using phpdocx 4.1 (Corp. license) and I've got a strange problem while using importHeadersAndFooters.

What I'm doing is basically easy .... importing header and footer, reading the html response from a page that works with diffedent db data and create the final doc.

The import seems to work fine but I cannot replace the variable in the footer using the classic replaceVariableByText.

I've read on this forum that I cannot do this with newwordfrag.

I'll attach here my snippet. 
Just to let you know, this script will work without the "variable replacement" part.

I hope someone will be so kind to help me.

Best regards,
Frank

<?php
//phpdocx lib
require_once ("../phpdocx/classes/CreateDocx.inc");

$docx = new CreateDocx();

//the docx file with $VARIABLE$ in the footer
$docx->importHeadersAndFooters('footerheader.docx');

$variables = array('VARIABLE' => 'example');
$docx->replaceVariableByText($variables);

ob_start();
  include('print.php');
  $page = ob_get_contents();
ob_end_clean();

$docx->embedHTML($page , array('downloadImages' => true ));
ob_end_clean(); 

$docx->createDocxAndDownload('upload/test');

?>

 

Posted by admin  · 17-11-2015 - 18:08

Hello,

The support of placeholders replacement in headers and footers by WordFragments and HTML was added in phpdocx 4.6, so we recommend you to upgrade to use this feature.

About your other question, you need to set the target parameter to 'footer' or 'header' to replace the correct scope. Please check the documentation available on:

http://www.phpdocx.com/api-documentation/templates/replace-variable-text-Word-document

Regards.

Posted by campagnuolo  · 18-11-2015 - 08:32

Hello again,

thank you for your fast reply.

 

To be honest, I've tried your solution some days ago without any success!

Using the key "target" I was able to achieve my result but only if I replace this

 

$docx = new CreateDocx();

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

 

with this

 

$docx = new CreateDocxFromTemplate('footerheader.docx');   

 

The footerheader.docx is just a void body template with header&footer (header img, footer test,pagination and 1 variable)

I don't know if this is a bug/mistake/something that afflicts 4.1 but after I import header and footer with "importHeadersAndFooters" I cannot use anymore replacevariablebytext (and this is why I asked for fragment too).

Do you think I can use my solution ( using " CreateDocxFromTemplate")  or I have to find a workaround to implement importHeadersAndFooters anyway ?

 

I really appreciate your help.

 

I hope to hear from you soon

Posted by admin  · 18-11-2015 - 10:41

Hello,

We recommend you to do it in two steps: first create a document, import the headers/footers and save it; and then open it again using the CreateDocxFromTemplate class and replace the placeholders

The method replaceVariableByText is not available in the CreateDocx class.

Regards.