I want to load a .docx template, replace some tags and save the document. The first test with just opening and immediately saving the document without changing anything fails because all the images are positioned and sized wrong in the generated file. They also seem to lose the position "behind text".
The expected behavior is, that the input and output files should look the same.
This is the template file: https://new.myexperts.ch/demo/STSE_Expertenbericht_Vorlage.docx
And this is the resulting file after loading it with PhpWord: https://new.myexperts.ch/demo/STSE_Report.docx
This is the code I used:
require_once '/home/myexperts/domains/new.myexperts.ch/public_html/vendor/autoload.php';
require_once '/home/myexperts/domains/new.myexperts.ch/public_html/vendor/phpoffice/phpword/bootstrap.php';
// Load existing .docx file
$phpWord = \PhpOffice\PhpWord\IOFactory::load('/home/myexperts/domains/new.myexperts.ch/public_html/template/STSE_Expertenbericht_Vorlage.docx');
// Save the new file as a temporary file on the server
$temp_file = tempnam(sys_get_temp_dir(), 'PhpWord');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($temp_file);
// Output headers and the file for download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename('STSE_Report.docx'));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($temp_file));
flush();
readfile($temp_file);
unlink($temp_file); // Deletes the temporary file
exit;
Thank you!