Open abuzer opened 2 years ago
You need to use some external tool, like LibreOffice. PHPWord's docx to pdf transformation is very limited.
You need to use some external tool, like LibreOffice. PHPWord's docx to pdf transformation is very limited.
I saw your reply on a different issue as well that you used libre to overcome the limitations of phpword, could you share a tutorial/article on how to do it? A gist would also be welcome :)
Well first you need to install libreoffice on your machine, then you can just take a look at the manual page https://www.systutorials.com/docs/linux/man/1-soffice/ . The command you are looking for is --convert-to. I also suggest you to add the --headless --invisible and --norestore options, since you are just going for a simple conversion, to make the process lighter and more stable.
install with composer mpdf/mpdf
// 1 step. open template, replace and save docx
$templateProcessor = new TemplateProcessor('your_template_file.docx');
$templateProcessor->setValues($replace_array);
$filepath = $templateProcessor->saveAs('output_file.docx');
// pdf settings
$rendererName = Settings::PDF_RENDERER_MPDF;
$rendererLibraryPath = realpath('../vendor/mpdf/mpdf');
Settings::setPdfRenderer($rendererName, $rendererLibraryPath);
// 2 step. open docx, saveAs pdf
$phpWord = IOFactory::load('output_file.docx');
$objWriter = IOFactory::createWriter($phpWord, 'PDF');
$objWriter->save('output_file.pdf');
install with composer mpdf/mpdf
// 1 step. open template, replace and save docx $templateProcessor = new TemplateProcessor('your_template_file.docx'); $templateProcessor->setValues($replace_array); $filepath = $templateProcessor->saveAs('output_file.docx'); // pdf settings $rendererName = Settings::PDF_RENDERER_MPDF; $rendererLibraryPath = realpath('../vendor/mpdf/mpdf'); Settings::setPdfRenderer($rendererName, $rendererLibraryPath); // 2 step. open docx, saveAs pdf $phpWord = IOFactory::load('output_file.docx'); $objWriter = IOFactory::createWriter($phpWord, 'PDF'); $objWriter->save('output_file.pdf');
The linked liberary's DOCX to PDF conversion is very limited, similar to PHPWord's.
ValueError: Invalid or uninitialized Zip object in ZipArchive->getFromName() (line 65 of modules/custom/rental_agreement/vendor/phpoffice/phpword/src/PhpWord/Shared/XMLReader.php).
use PhpOffice\PhpWord\IOFactory;
$temporary_file_path = "public://uploads/52023-06-21.docx";
$phpWord = IOFactory::load($temporary_file_path, 'Word2007');
$objWriter = IOFactory::createWriter($phpWord, 'PDF');
$objWriter->save('public://uploads/52023-06-21.pdf');
I am working on Drupal 10 Custom Modules development. Any Solution? PHP : - 8.2.7v Drupal:- 10.0.9v
We are trying to read from docx and replace the variable. But want to save the output as pdf.
Tried:
But the output PDF is a mess.