Skip to content

Instantly share code, notes, and snippets.

@uoxiu
Created October 22, 2015 09:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uoxiu/55ed39e7d359fe7b4533 to your computer and use it in GitHub Desktop.
Save uoxiu/55ed39e7d359fe7b4533 to your computer and use it in GitHub Desktop.
<?php
$folder = 'W:\\test\\';
$infile = $folder . 'in.docx';
$outFile = $folder . 'out.docx';
$targetFiles = [
'word/document.xml'
];
$fields = [
'{COMPANY-NAME}' => 'CREDITEXXXXXXXXXXXX'
];
if (copy($infile, $outFile) == false) {
die("failed to copy");
}
$zipIn = new ZipArchive;
$zipOut = new ZipArchive;
if ($zipIn->open($infile) === TRUE && $zipOut->open($outFile, ZipArchive::CREATE) === TRUE) {
foreach ($targetFiles as $targetFile) {
$newContents = $zipIn->getFromName($targetFile);
foreach ($fields as $fieldKey => $fieldValue) {
$newContents = str_replace($fieldKey, $fieldValue, $newContents);
}
$zipOut->deleteName($targetFile);
$zipOut->addFromString($targetFile, $newContents);
}
$zipIn->close();
$zipOut->close();
echo 'generated';
} else {
echo 'failed';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment