Create PDF from .docx generated by PHPWord

I am creating .docx files from a template using PHPWord . It works fine, but now I want to convert the generated file to PDF .

At first I tried using tcpdf in combination with PHPWord

 $wordPdf = \PhpOffice\PhpWord\IOFactory::load($filename.".docx"); \PhpOffice\PhpWord\Settings::setPdfRendererPath(dirname(__FILE__)."/../../Office/tcpdf"); \PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF'); $pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF'); if (file_exists($filename.".pdf")) unlink($filename.".pdf"); $pdfWriter->save($filename.".pdf"); 

but when I try to upload a file to convert it to PDF , I get the following exception when uploading a file

Fatal error: Throw a "BadMethodCallException" exception with the message "Cannot add PreserveText to section".

After some research, I found that some others also have this error ( phpWord - Cannot add PreserveText to section )

EDIT

After I tried a little more, I found that Exception only occurs when there are mail merge fields in my document. As soon as I deleted them, Exception no longer appears, but the converted PDF files look awful. All information about the style has disappeared, and I can’t use the result, so the need for an alternative remains.



I was thinking of using another way to create a PDF file, but I could only find 4 ways:

  • Using OpenOffice . It is impossible, since I cannot install any software on the Server. In addition, the mentioned method here did not work either as my host ( Strato ) uses SunOS as an OS, and this requires Linux
  • Using phpdocx - I don’t have a budget to pay, and the demo cannot create a PDF
  • Using PHPLiveDocx - it works, but has a limit of 250 documents per day and 20 per hour, and I have to convert about 300 documents at the same time, maybe even several times a day.
  • Using PHP-Digital-Format-Convert - the output looks better than with PHPWord and tcpdf , but is still unavailable because there are no images, and most (not all!) Styles

Is there a 5th way to create a PDF file? Or is there any solution so that the generated PDFs look good?

+9
php pdf docx phpword
source share
4 answers

I used Gears / pdf to convert the docx file generated by phpword to PDF:

 $success = Gears\Pdf::convert( 'file_path/file_name.docx', 'file_path/file_name.pdf'); 
0
source share

You are trying to detach a PDF file before saving it, and you also need to unlink the DOCX document, not the PDF document.

Try this.

 $pdfWriter = \PhpOffice\PhpWord\IOFactory::createWriter($wordPdf , 'PDF'); $pdfWriter->save($filename.".pdf"); unlink($wordPdf); 
0
source share

Try this:

 // get the name of the input PDF $inputFile = "C:\\PHP\\Test1.docx"; // get the name of the output MS-WORD file $outputFile = "C:\\PHP\\Test1.pdf"; try { $oLoader = new COM("easyPDF.Loader.8"); $oPrinter = $oLoader->LoadObject("easyPDF.Printer.8"); $oPrintJob = $oPrinter->PrintJob; $oPrintJob->PrintOut ($inputFile, $outputFile); print "Success"; } catch(com_exception $e) { Print "error code".$e->getcode(). "\n"; print $e->getMessage(); } 
0
source share

I do not think that I am right. You save the document as HTML content

$ objWriter = \ PhpOffice \ PhpWord \ IOFactory :: createWriter ($ phpWord, 'HTML');

After reading the contents of the HTML file and writing the contents as a PDF file using mPDF or tcPdf or fpdf.

-one
source share

All Articles