Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- header("Content-type: application/vnd.ms-word");
- header("Content-Disposition: attachment;Filename=document_name.doc");
- echo "<html>";
- echo "<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">";
- echo "<body>";
- echo "<b>My first document</b>";
- echo "</body>";
- echo "</html>";
- <?php
- function fWriteFile($sFileName,$sFileContent="No Data",$ROOT)
- {
- $word = new COM("word.application") or die("Unable to instantiate Word");
- //bring it to front
- $word->Visible = 1;
- //open an empty document
- $word->Documents->Add();
- //do some weird stuff
- $word->Selection->TypeText($sFileContent);
- $word->Documents[1]->SaveAs($ROOT."/".$sFileName.".doc");
- //closing word
- $word->Quit();
- //free the object
- $word = null;
- return $sFileName;
- }
- ?>
- <?php
- $PATH_ROOT=dirname(__FILE__);
- $Return ="<table>";
- $Return .="<tr><td>Row[0]</td></tr>";
- $Return .="<tr><td>Row[1]</td></tr>";
- $sReturn .="</table>";
- fWriteFile("test",$Return,$PATH_ROOT);
- ?>
- function mailMerge($templateFile, $newFile, $row)
- {
- if (!copy($templateFile, $newFile)) // make a duplicate so we dont overwrite the template
- return false; // could not duplicate template
- $zip = new ZipArchive();
- if ($zip->open($newFile, ZIPARCHIVE::CHECKCONS) !== TRUE)
- return false; // probably not a docx file
- $file = substr($templateFile, -4) == '.odt' ? 'content.xml' : 'word/document.xml';
- $data = $zip->getFromName($file);
- foreach ($row as $key => $value)
- $data = str_replace($key, $value, $data);
- $zip->deleteName($file);
- $zip->addFromString($file, $data);
- $zip->close();
- return true;
- }
- $replacements = array('[Person Name]' => 'Mina', '[Person Last Name]' => 'Mooo');
- $newFile = tempnam_sfx(sys_get_temp_dir(), '.dat');
- $templateName = 'personinfo.docx';
- if (mailMerge($templateName, $newFile, $replacements))
- {
- header('Content-type: application/msword');
- header('Content-Disposition: attachment; filename=' . $templateName);
- header('Accept-Ranges: bytes');
- header('Content-Length: '. filesize($file));
- readfile($newFile);
- unlink($newFile);
- }
- echo "<meta http-equiv="Content-Type" content="text/html; charset=utf-8">";
Add Comment
Please, Sign In to add comment