Php and Word mail merge
didier dany
-
jamespage -
jamespage -
Hello everyone
I would like to extract data from a MySQL database using a PHP script to create a personalized letter with Word (for example, a mail merge with an existing template letter). Note: I generate a query from the remote site.
Thank you for your help.
didier dany
I would like to extract data from a MySQL database using a PHP script to create a personalized letter with Word (for example, a mail merge with an existing template letter). Note: I generate a query from the remote site.
Thank you for your help.
didier dany
10 answers
For now, I have the solution:
Create a Word document with fields like {{NAME}}
save it as RTF and then use the following PHP code (you can put this code in a loop and use the print function from JavaScript to print page by page, I do it without any problem!!!
<?php
// Letter
$f=fopen("./word/InputFile.rtf","r");
$R=fopen("./word/OutputFile.doc","w");
while (!feof($f))
{
$line=fgets($f,20000);
$Length=strlen($line);
$NewLine="";
for ($i=$inc;$i<$Length;$i++)
{
if (substr($line,$i,4)=="\{\{")
{
$start=$i;
$var="";
for ($j=$i+4;$j<$Length;$j++)
{
if (substr($line,$j,4)=="\}\}")
{
$end=$j;
break;
}
}
$var=substr($line,$start+4,$end-$start-4);
if ($var=="NAME") $NewLine.=$UserName;
else if ($var=="NAME") $NewLine.=$Name;
else if ($var=="FIRSTNAME") $NewLine.=$Firstname;
else if ($var=="ADDRESS") $NewLine.=$Address;
else if ($var=="ADDRESS_CPL") $NewLine.=$Complete_Address;
else if ($var=="ZIP") $NewLine.=$Address_ZIP;
else if ($var=="CITY") $NewLine.=$Address_City;
else if ($var=="DATE") $NewLine.=date("d/m/Y");
else if ($var=="CIV") $NewLine.=$Civ;
else $NewLine.="???????";
$i=$j+3;
}
else
{
$NewLine.=substr($line,$i,1);
}
}
fwrite($R,$NewLine);
}
fclose($f);
fclose($R);
?>
Create a Word document with fields like {{NAME}}
save it as RTF and then use the following PHP code (you can put this code in a loop and use the print function from JavaScript to print page by page, I do it without any problem!!!
<?php
// Letter
$f=fopen("./word/InputFile.rtf","r");
$R=fopen("./word/OutputFile.doc","w");
while (!feof($f))
{
$line=fgets($f,20000);
$Length=strlen($line);
$NewLine="";
for ($i=$inc;$i<$Length;$i++)
{
if (substr($line,$i,4)=="\{\{")
{
$start=$i;
$var="";
for ($j=$i+4;$j<$Length;$j++)
{
if (substr($line,$j,4)=="\}\}")
{
$end=$j;
break;
}
}
$var=substr($line,$start+4,$end-$start-4);
if ($var=="NAME") $NewLine.=$UserName;
else if ($var=="NAME") $NewLine.=$Name;
else if ($var=="FIRSTNAME") $NewLine.=$Firstname;
else if ($var=="ADDRESS") $NewLine.=$Address;
else if ($var=="ADDRESS_CPL") $NewLine.=$Complete_Address;
else if ($var=="ZIP") $NewLine.=$Address_ZIP;
else if ($var=="CITY") $NewLine.=$Address_City;
else if ($var=="DATE") $NewLine.=date("d/m/Y");
else if ($var=="CIV") $NewLine.=$Civ;
else $NewLine.="???????";
$i=$j+3;
}
else
{
$NewLine.=substr($line,$i,1);
}
}
fwrite($R,$NewLine);
}
fclose($f);
fclose($R);
?>
Listen, I'm sorry, but no one has an answer to your question. However, since it's been a while, if you've made any progress on the matter, I would be interested in getting some information.
:)
:)
Hi!
I'm looking to do the same thing ......
Check out:
http://www.npds.org/viewtopic.php?topic=2666&forum=5
There's a PHP script that creates a Word (or Excel) file from a SQL query (a few modifications to make, it's mentioned in the forum)
But as for the mail merge, I don't know .....
See you!
I'm looking to do the same thing ......
Check out:
http://www.npds.org/viewtopic.php?topic=2666&forum=5
There's a PHP script that creates a Word (or Excel) file from a SQL query (a few modifications to make, it's mentioned in the forum)
But as for the mail merge, I don't know .....
See you!
Hello,
I know this script and I have already worked on it; in fact, it is of no interest because it doesn't work, it needed to be rewritten.
I am looking for a solution with the insertion of PHP (as a host language in VBA) to make it a macro.
This is an idea that crossed my mind, not necessarily a good one.
Thank you anyway, it's nice.
Didier
I know this script and I have already worked on it; in fact, it is of no interest because it doesn't work, it needed to be rewritten.
I am looking for a solution with the insertion of PHP (as a host language in VBA) to make it a macro.
This is an idea that crossed my mind, not necessarily a good one.
Thank you anyway, it's nice.
Didier
Hi!
What? The script works very well, I'm using it!
There are just a couple of syntax errors.
By the way, if you have a solution for mail merge.........
See you!
Titom
What? The script works very well, I'm using it!
There are just a couple of syntax errors.
By the way, if you have a solution for mail merge.........
See you!
Titom
One can also create XML documents. It's enough to invent some template strings that will need to be parsed with the extracted data. For example, you can save your letter in XML format, retrieve the XML letter with a PHP script, and proceed with the replacement using a library like SimpleXML... With each save, you add a page break, and there you go... next save.
* https://xml.developpez.com/
* http://ch2.php.net/simplexml
* http://rep.oio.dk/Microsoft.com/officeschemas/welcome.htm
* https://xml.developpez.com/
* http://ch2.php.net/simplexml
* http://rep.oio.dk/Microsoft.com/officeschemas/welcome.htm
I would like to (via a php script) generate a .doc from 2 files:
- template.doc: this is the formatting of my final document. With the fields to be filled in between quotes.
- file.xls: all the data that my fields can take.
With Word, if I click on merge, it generates my Word file with the template repeated as many times as there are rows in my .xls with the formatting of the latter.
I would like my php code to do the same (for now I have only generated the xls file with an export of data from my database)
If you have any idea and if you understood everything ;)
Thank you.
A very simple solution that I propose here:
http://html5.immo-scope.com/index.php?page=php/publiPostageDocumentWord
Best regards