Forum


Replies: 11   Views: 3692
Merge documents
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 mporter42  · 28-06-2011 - 14:49

I would like to build 1-N documents using a template and join them to one file. is that possible?

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

Hello,

Yes, you can merge 1-N DOCX using addDOCX method. Please check the following examples included in phpdocx pro:

· examples/easy/TemplateDOCX.php

· examples/easy/DOCX.php

Regards.

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

Hi,

[u][i][b]this simple code doesn't work[/b][/i][/u]

include_once('./helpers/phpdocx/classes/CreateDocx.inc');
$NomDocument = 'blabla';
$folder = "";

$docx = new CreateDocx();
$docx->setEncodeUTF8();
$Template = "doctmp.docx";
$docx->addTemplate($Template);
$docx->createDocx('tmpdoc');

$docx = new CreateDocx();
$docx->setEncodeUTF8();
$docx->addText('2 DOCX content');
$docx->createDocx('doctmp');


$docx = new CreateDocx();
$docx->setEncodeUTF8();
$docx->addText('DOCX content');
$docx->addDOCX('tmpdoc.docx');
$docx->addDOCX('doctmp.docx');
$docx->addText('reDOCX content');
$docx->createDocx($NomDocument);

[u][b][i]Using addtemplate is the problem
same code without addtemplate work fine[/i][/b][/u]

Do you know this issue ?

thx

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

Hi,

Thanks. We're going to check that issue.

Regards.

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

I have the same problem and it's very annoying :(

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

Hi,

My experience, on phpdocx, is that template document works very well just if you use it only once, if you want to chain two or more, it doesn't work, probably it's due to some missing reinitialisation, but i don't have time to search where...

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

Hi,

After checking this snippet, we've found a bug in phpdocx that bad-forms the documents once you add a template.

These are the patches that you must put in the library and that we'll include in the next phpdocx release:

[code]--- CreateTemplate.inc
+++ CreateTemplate.inc
@@ -236,6 +236,18 @@
{
return self::$_templateSymbol;
}
+
+ /**
+ * Setter. Change if DOCX is a template
+ *
+ * @access public
+ * @return void
+ * @static
+ */
+ public static function setBlnTemplate($enable)
+ {
+ self::$_template = $enable;
+ }
[/code]

[code]--- CreateDocx.inc
+++ CreateDocx.inc
@@ -2380,6 +2380,7 @@
} else {
throw new Exception('Unable to create DOCX file.');
}
+ CreateTemplate::setBlnTemplate(false);
}
catch (Exception $e) {
self::$log->fatal($e->getMessage());
[/code]

By the way, there is an "error" on MVa's code:

[code]@@ -15,13 +15,13 @@
$docx = new CreateDocx();
$docx->setEncodeUTF8();
$docx->addText('2 DOCX content');
-$docx->createDocx('doctmp');
+$docx->createDocx('doctmp_other_name');

$docx = new CreateDocx();
$docx->setEncodeUTF8();
$docx->addText('DOCX content');
$docx->addDOCX('tmpdoc.docx');
-$docx->addDOCX('doctmp.docx');
+$docx->addDOCX('doctmp_other_name.docx');
$docx->addText('reDOCX content');
$docx->createDocx($NomDocument);
?> [/code]

Regards.

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

HI,

I tried your patch this morning, it's working fine!!

good job!

thx

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

Thanks for this patch :)

In an other part, is there a solution for this (last post) : http://www.phpdocx.com/forum?vasthtmlaction=viewtopic&t=55.00 ?

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

[quote][b]Quote from nicolas on July 18, 2011, 08:51[/b]
Thanks for this patch :)

In an other part, is there a solution for this (last post) : http://www.phpdocx.com/forum?vasthtmlaction=viewtopic&t=55.00 ?[/quote]
This is the only solution i had but it works fine

include_once(’./helpers/phpdocx/classes/CreateDocx.inc’);
$NomDocument = ‘blabla’;
$folder = “”;

$docx = new CreateDocx();
$docx->setEncodeUTF8();
$Template = “doctmp.docx”;
$docx->addTemplate($Template);
$docx->createDocx(’tmpdoc’);

$docx = new CreateDocx();
$docx->setEncodeUTF8();
$docx->addText(’2 DOCX content’);
$docx->createDocx(’doctmp_2’);

$docx = new CreateDocx();
$docx->setEncodeUTF8();
$Template = “doctmp_1.docx”;
$docx->addTemplate($Template);
$docx->createDocx(’tmpdoc_2’);


$docx = new CreateDocx();
$docx->setEncodeUTF8();
$docx->addText(’DOCX content’);
$docx->addDOCX(’tmpdoc.docx’);
$docx->addDOCX(’doctmp_2.docx’);
$docx->addDOCX(’tmpdoc_2.docx’);
$docx->addText(’reDOCX content’);
$docx->createDocx($NomDocument);

In my application I add more than 50 documents made with different template and it work fine and fast

++

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

Before the patch I already tried your solution but it didn't work, that's why I follow your forum thread and apply the patch.
Although, the patch work for now, I think that the Addtemplate() method could be modified in order to be used in a current document. It would be easier, faster and cleaner :)
So, next version of phpdocx could change the Addtemplate() method ?