PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Wordfile generated from template results in broken background images

Open ChrisPrefect opened this issue 2 years ago • 2 comments

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".

This is the template file: Template file

original

And this is the resulting file after loading it with PhpWord: Output file from PHPWord

wrong output

This is the code I used to test:

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;

The expected behavior is, that the input and output files should look the same.

Currently, the images in the output file are placed and sized wrongly.

Context

Please fill in your environment information:

  • PHP Version: 8.2.11
  • PHPWord Version: 1.1.0

ChrisPrefect avatar Nov 04 '23 17:11 ChrisPrefect

@ChrisPrefect The template is not available. Could you upload the file ?

Progi1984 avatar Sep 03 '24 07:09 Progi1984