Forum


Replies: 4   Views: 3522
Addtemplateimage not working
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 mbcalvin  · 02-10-2012 - 19:48

Hello,
I am trying to dynamically change an image in my template. I have added a placeholder image and right-clicked, went to the web tab, and added my tag to the alt text.

My directory structure is root/site/index.php and my images are at root/public/images/pharmacy_logos/image.jpg, and here is the code I am using:

[code]
require $_SERVER['DOCUMENT_ROOT']. "/public/phpdocx/classes/CreateDocx.inc";

error_reporting(E_STRICT);
ini_set('display_errors', 1);

$data = array('employee'=> $employee->get_full_name(), 'date' => date('m/d/Y'), 'department'=>ucwords(strtolower($employee->get_department_name()))); //these are constant values
$docx = new CreateDocx(); //instantiates document object for use on subsequent pages.
$url = explode('/',req('q'));
$template = $url[count($url)-1]; //This give me the template name properly
$filename = $employee->get_employee_id() . $template;
$docx->addTemplate(DOC_LOCATION . '/templates/' . $template . ".docx"); //set the word doc template
$docx->setTemplateSymbol('~'); //use the ~ as the variable delimiter because $ is used in some templates
$docx->setDefaultFont('Century Gothic');
$vars = $docx->getTemplateVariables(); //get the defined variables on the template

$docx->addTemplateImage('logo', $_SERVER['DOCUMENT_ROOT'] . '/public/images/pharmacy_logos/29.jpg'); //this is statically set for testing

foreach ($vars['document'] as $k => $v) //compare the template variables to the keys of the data array
{
if (array_key_exists($v, $data))
{
//echo "setting $v to " . $data[$v] . '<br />';
$docx->addTemplateVariable($v, $data[$v], array());
}
}

$docx->createDocxAndDownload($filename);
[/code]

everything is working perfectly except for the image swapping. Additionally, I tried to use your image example and it worked, so it is not a server problem. Do I need to do more than just add the tag to the alt-text or something?

Posted by mbcalvin  · 11-04-2013 - 12:13

I forgot to add, the log shows that everything is working correctly.

[code]
10/02/12 12:37:02,279 [7996] INFO phpdocx_logger - Create a temp file to use as initial ZIP file. DOCX is a ZIP file.
10/02/12 12:37:02,392 [7996] INFO phpdocx_logger - The language has been set.
10/02/12 12:37:02,411 [7996] INFO phpdocx_logger - Open template [siteroot]/public/docs//templates/chain_cover.docx.
10/02/12 12:37:02,413 [7996] INFO phpdocx_logger - Change template symbol.
10/02/12 12:37:02,415 [7996] INFO phpdocx_logger - Change default font.
10/02/12 12:37:02,415 [7996] INFO phpdocx_logger - Return template variables.
[b]10/02/12 12:37:02,424 [7996] INFO phpdocx_logger - Assign and replace image variable logo in template.[/b]
10/02/12 12:37:02,426 [7996] INFO phpdocx_logger - Assign and replace text variable barcode in template.
10/02/12 12:37:02,427 [7996] INFO phpdocx_logger - Assign and replace text variable date in template.
10/02/12 12:37:02,428 [7996] INFO phpdocx_logger - Assign and replace text variable patient_first in template.
10/02/12 12:37:02,428 [7996] INFO phpdocx_logger - Assign and replace text variable patient_last in template.
10/02/12 12:37:02,429 [7996] INFO phpdocx_logger - Assign and replace text variable patient_id in template.
10/02/12 12:37:02,430 [7996] INFO phpdocx_logger - Assign and replace text variable patient_addr1 in template.
10/02/12 12:37:02,430 [7996] INFO phpdocx_logger - Assign and replace text variable patient_addr2 in template.
10/02/12 12:37:02,431 [7996] INFO phpdocx_logger - Assign and replace text variable patient_city in template.
10/02/12 12:37:02,432 [7996] INFO phpdocx_logger - Assign and replace text variable patient_state in template.
10/02/12 12:37:02,433 [7996] INFO phpdocx_logger - Assign and replace text variable patient_zip in template.
10/02/12 12:37:02,433 [7996] INFO phpdocx_logger - Set DOCX name to: [siteroot]/public/docs/0BMchain_cover.
10/02/12 12:37:02,433 [7996] DEBUG phpdocx_logger - DOCX is a template.
10/02/12 12:37:02,433 [7996] INFO phpdocx_logger - Create a temp copy of the file, to generate a new DOCX.
10/02/12 12:37:02,435 [7996] INFO phpdocx_logger - Document.xml.rels exists, replace word/_rels/document.xml.rels with a new document.
10/02/12 12:37:02,436 [7996] INFO phpdocx_logger - Replace [Content_Types].xml with a new document.
10/02/12 12:37:02,436 [7996] INFO phpdocx_logger - Replace word/_rels/document.xml.rels with a new document.
10/02/12 12:37:02,442 [7996] INFO phpdocx_logger - Replace word/document.xml with a new document.
10/02/12 12:37:02,443 [7996] INFO phpdocx_logger - Add embedded files.
10/02/12 12:37:02,443 [7996] INFO phpdocx_logger - End of file, close it.
10/02/12 12:37:02,451 [7996] INFO phpdocx_logger - Download file 0BMchain_cover.docx.
[/code]

The image doesn't get replaced by anything though.

Posted by admin  · 11-04-2013 - 12:13

Hello,

Please contact us to test your issue.

Regards.

Posted by chrisstalman  · 11-07-2013 - 17:08

Hi, I had the same issue. It took me a while, but I found out that my template was made with MS Word 2003. After I edited it in Word 2007, it worked!



Word 2003 saved as docx stores the alt text tag in a different way than the 2007 version.



Hope this helpes others who are looking for a solution to this problem.