PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.27k stars 2.69k forks source link

How to save/export fillable templateProcessor form docx to pdf? #2180

Open abuzer opened 2 years ago

abuzer commented 2 years ago

We are trying to read from docx and replace the variable. But want to save the output as pdf.

echo date('H:i:s'), ' Saving the result document...', EOL;
$templateProcessor->saveAs('results/Sample_40_TemplateSetComplexValue.docx');

echo getEndingNotes(array('Word2007' => 'docx'), 'results/Sample_40_TemplateSetComplexValue.docx');

Tried:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$phpWord = \PhpOffice\PhpWord\IOFactory::load($docxPath); 

$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save($pdfPath); 

But the output PDF is a mess.

Jack-ST94 commented 2 years ago

You need to use some external tool, like LibreOffice. PHPWord's docx to pdf transformation is very limited.

martonkaufmann commented 2 years ago

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 :)

Jack-ST94 commented 2 years ago

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.

maxrostov commented 2 years ago

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');
Jack-ST94 commented 2 years ago

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.

Sidhota54 commented 1 year ago

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