-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot save a word document as PDF #2426
Comments
You have to set the reader first, its either MPDF or DomPDF \PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_MPDF);
$phpWord = \PhpOffice\PhpWord\IOFactory::load(storage_path('generated.docx'),);
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF');
$xmlWriter->save(storage_path('generated.pdf')); |
@allestaire to even better simulate, I have attached the word document it should change and save as pdf here: https://filetransfer.io/data-package/WNmNVR95#link the full code I currently use: <?php
use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\TemplateProcessor;
$domPdfPath = base_path('vendor/dompdf/dompdf');
Settings::setPdfRendererPath($domPdfPath);
Settings::setPdfRendererName('DomPDF');
$templateFile = storage_path('app/documents/source_files/Acceptatiebrief.docx');
$templateProcessor = new TemplateProcessor($templateFile);
$templateProcessor->setValue('{teamnr}', '123');
$templateProcessor->setValue('{strt_hr}', '18:31');
$templateProcessor->setValue('{pilot_name}', 'DOE John');
$templateProcessor->setValue('{pilot_license}', 'W123456R');
$templateProcessor->setValue('{pilot_club}', 'Amazing club');
$templateProcessor->setValue('{pilot_tel}', '+333333333');
$templateProcessor->setValue('{copilot_name}', 'DOEDETTE Johanna');
$templateProcessor->setValue('{copilot_license}', 'A65412D');
$templateProcessor->setValue('{copilot_club}', 'Woops');
$templateProcessor->setValue('{copilot_tel}', '');
$outputFileDocx = storage_path('app/documents/output_files/Acceptatiebrief_test.docx');
$templateProcessor->saveAs($outputFileDocx);
\PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF);
$phpWord = \PhpOffice\PhpWord\IOFactory::load($outputFileDocx);
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF');
$xmlWriter->save(storage_path('app/documents/output_files/Acceptatiebrief_test.pdf')); Any ideas on what I'm doing wrong? |
Seems like there is a problem with the docx format, Also can you share the original docx? then converting docx to pdf is not totally reliable, as I tested it, it loses styles, and there are only I guess 80% of accuracy |
@allestaire https://filetransfer.io/data-package/WNmNVR95#link this url contains the original docx file (with just the {replace} tags used. |
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.
Expected Behavior
I expect the word to be saved as a pdf file.
Current Behavior
Exception gets throwed:
Context
Please fill in your environment information:
The text was updated successfully, but these errors were encountered: