Skip to content
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

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

Open
abuzer opened this issue Jan 6, 2022 · 6 comments
Open

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

abuzer opened this issue Jan 6, 2022 · 6 comments

Comments

@abuzer
Copy link

abuzer commented Jan 6, 2022

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
Copy link

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

@martonkaufmann
Copy link

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
Copy link

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
Copy link

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
Copy link

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
Copy link

Sidhota54 commented Jun 22, 2023

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:https://uploads/52023-06-21.docx";
$phpWord = IOFactory::load($temporary_file_path, 'Word2007');
$objWriter = IOFactory::createWriter($phpWord, 'PDF');
$objWriter->save('public:https://uploads/52023-06-21.pdf');

I am working on Drupal 10 Custom Modules development. Any Solution?
PHP : - 8.2.7v Drupal:- 10.0.9v

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants