Forum


Replies: 2   Views: 3828
Merging docx files - page numbering
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 rjonge  · 23-11-2011 - 11:19

Hello,

I am working with templates and want to make a batch file of more documents. For example we've got 50 letters for different persons. We don't want to press a button 50 times to download and print each document seperatly. We want to merge those 50 documents into one document and download it to print it at once. I've got a page number in the template footer.

For now i use the method addDOCX to combine more documents. So first i will generate a docx file for each template and later on i will combine everything with the addDOCX method. Each genereted docx files has 2 pages. Page numbering is working fine for now for each document.

But when I combine 50 of these seperate documents with the addDOCX method, the last page has page number 100. I was hoping for a possibility to merge more documents and keep exactly the format of those documents. So the first page of the merged document has pagenumber 1, the second page pagenumber 2. But the 3th page has pagenumber 1 again. (this is the first page of the secong docx file) and so on.

So i want to merge more documents in one document only to make printing easier and faster for the end user.

Has anybody a good solution?


example code:
---------------------------------------------
[code]
...
//includes for dbase, phpdocx etc.

$oDatabase->Query("SELECT `ID`, `templateFileName`, `name` FROM `Docs` WHERE `printed` = false LIMIT 50 ");

//loop 50 documents and make a docx file for each record.
while($aDocs = $oDatabase->NextRow())
{

$oDocx = new CreateDocx();
$oDocx->addTemplate($aDocs["templateFileName"]); //the template has 2 pages
$oDocx->addTemplateVariable('varname', $aDocs["name"]);
$oDocx->createDocx($aDocs["ID"].'-generateddoc');

//save the ID's of the documents we have generated in this batch
$aGeneratedDocID[] = $aDocs["ID"];
}

//loop the documents ID's and make one file.
$oDocx = new CreateDocx();
foreach($aGeneratedDocID AS $iDocNumber)
{
$oDocx->addDOCX($iDocNumber . 'generateddoc.docx');
$oDocx->addBreak('page');
}
$oDocx->createDocx('batchfile');

//for testing purpose i didn't cleanup the code. I could use a second docx object to combine the documents in the first while loop instead of using the $aGeneratedDocID variabele.
[/code]

[b]Result[/b]
a batchfile.docx with 50 seperate documents (for seperate customers) with page numbering till 100. and not 1,2,1,2,1,2 and so on.

Is there a solution?

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

I see more format of the original docx file is lost when i use the addDOCX() method.

If I have got 3 docx files with each other marging top values.
doc1: marging-top = 4 cm
doc2: marging-top = 10 cm
doc3: marging-top = 16 cm

with the addDOCX function i combine these three
$docx = new CreateDocx();
$docx->addDOCX('doc1.docx');
$docx->addDOCX('doc2.docx');
$docx->addDOCX('doc3.docx');
$docx->createDocx('batch');

The batch.docx file has indead 3 pages but all the pages will start at the top of the page. (the original margins are lost)