Closed
Description
Describe the Bug
When trying to modify a word document (has a header and footer text in it) then the modification works perfectly. However, the word document needs to be a PDF. When I try to save it as PDF, I get Cannot add Title in Header.
Steps to Reproduce
Please provide a code sample that reproduces the issue.
<?php
require __DIR__ . '/vendor/autoload.php';
$templateFile = storage_path('app/wordDocument.docx');
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templateFile);
// set word values
$templateProcessor->setValue('{teamnr}', '123');
$outputFileDocx = storage_path('app/output_files/wordDocument.docx');
$templateProcessor->saveAs($outputFileDocx);
// load the word document again
$outputFilePdf = storage_path('app/output_files/pdf_file.pdf');
$phpWord = \PhpOffice\PhpWord\IOFactory::load($outputFileDocx);
//Save it into PDF
$PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord,'PDF');
$PDFWriter->save($outputFilePdf);
Expected Behavior
I expect the word to be saved as a pdf file.
Current Behavior
Exception gets throwed:
BadMethodCallException
Cannot add Title in Header.
at vendor\phpoffice\phpword\src\PhpWord\Element\AbstractContainer.php:270
266▕
267▕ // Check if a method is valid for current container
268▕ if (isset($validContainers[$method])) {
269▕ if (!in_array($this->container, $validContainers[$method])) {
➜ 270▕ throw new BadMethodCallException("Cannot add {$method} in {$this->container}.");
271▕ }
272▕ }
273▕
274▕ // Check if a method is valid for current container, located in other container
1 vendor\phpoffice\phpword\src\PhpWord\Element\AbstractContainer.php:135
PhpOffice\PhpWord\Element\AbstractContainer::checkValidity("Title")
2 vendor\phpoffice\phpword\src\PhpWord\Element\AbstractContainer.php:116
PhpOffice\PhpWord\Element\AbstractContainer::addElement("Title", Object(PhpOffice\PhpWord\Element\TextRun), "1")
Context
Please fill in your environment information:
- PHP Version: 8.2.8
- PHPWord Version: ^1.1
- windows 10 pro
- Laravel 10