Forum


Replies: 6   Views: 3654
Template, saves curropt file.
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 fortech  · 09-06-2011 - 13:58

I'm trying to do a simple variable replace with a template I created only to have it save a corrupt docx file. I've identified the issue.

The XML from my template looks like this:
[b]$[/b]</w:t></w:r><w:r w:rsidR="00C750CD" w:rsidRPr="00725C26"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/><w:sz w:val="20"/></w:rPr><w:t></w:t></w:r><w:proofErr w:type="spellStart"/><w:r w:rsidR="00C750CD" w:rsidRPr="00725C26"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/><w:sz w:val="20"/></w:rPr><w:t>[b]TEST[/b]</w:t></w:r><w:proofErr w:type="gramStart"/><w:r w:rsidR="00C750CD" w:rsidRPr="00725C26"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/><w:sz w:val="20"/></w:rPr><w:t></w:t></w:r><w:proofErr w:type="spellEnd"/><w:proofErr w:type="gramEnd"/><w:r w:rsidR="00C750CD" w:rsidRPr="00725C26"><w:rPr><w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial"/><w:sz w:val="20"/></w:rPr><w:t xml:space="preserve">[b]$[/b]

When it should look like [b]$TEST$[/b]. Word is adding in all this extra xml in the middle of my variables, making them unable to be found. Is there a special way to easily clean the Word xml file? Is there a clean function that can be run on the xml file before replacing the variables? I've tried several approaches at writing one with no success.

I'm using the latest version of Word.

Thanks.

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

Hello,

Phpdocx should solve this issue on its own, but you can avoid that spell tags opening the template, clearing the placeholder and rewriting it without using delete key.

Regards.

Posted by fortech  · 11-04-2013 - 12:12

I was taken of the off the doc creating part of the project, but now I'm back.

That appears to clean up the file, however I'm still getting corrupt files (1Kb compared to 23Kb as expected). After removing all my code I got:

$phpdocx = new CreateDocx();
$phpdocx->addTemplate('template.docx');
$phpdocx->createDocxAndDownload("test.docx");

and still got a corrupt file. I'm using Word 2010 on Windows 7 (if that helps). Any insight would be greatly appreciated. Thanks.

Posted by fortech  · 11-04-2013 - 12:12

Changing the corrupted doc file I get:
<br />
<b>Warning</b>: readfile(test.docx) [<a href='function.readfile'>function.readfile</a>]: failed to open stream: No such file or directory in <b>**root**/phpdocx/classes/CreateDocx.inc</b> on line <b>2432</b><br />

relating to:

public function createDocxAndDownload()
{
$args = func_get_args();

try {
$this->createDocx($args[0], $args[1]);
}
catch (Exception $e) {
self::$log->fatal($e->getMessage());
exit();
}

if (!empty($args[0])) {
$file_parts = explode("/", $args[0]);
$fileName = end($file_parts);
} else {
$fileName = 'document';
}

self::$log->info(
'Download file ' . $fileName . '.' . $this->_extension . '.'
);
header(
'Content-Type: application/vnd.openxmlformats-officedocument.' .
'wordprocessingml.document'
);
header(
'Content-Disposition: attachment; filename="' . $fileName .
'.' . $this->_extension . '"'
);
header('Content-Transfer-Encoding: binary');
readfile($fileName . '.' . $this->_extension); // <---- this line
}

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

Which version of phpdocx are you using?
Try to update to the last release of the library.

Posted by fortech  · 11-04-2013 - 12:12

Version 2.3

After overwriting all the files, docx file create properly. Awesome. Thanks.