2

I trying to convert file doc or docx to pdf but the result doesn't match with the origin file doc/docx and also there is no style in file pdf. I don't know why, because here i'm using tcpdf and phpword

this is my code to convert:

   $filetarget = FileHelper::normalizePath($pathdirectory.'/'.$filename);

   $objReader = \PhpOffice\PhpWord\IOFactory::createReader('Word2007');
   $contents = $objReader->load($filetarget);

   $tcpdfPath = Yii::getAlias('@baseApp') . '/vendor/tecnickcom/tcpdf';
   \PhpOffice\PhpWord\Settings::setPdfRendererPath($tcpdfPath);
   \PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
            
   $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($contents,'PDF');
   $fileresult = str_replace('.docx', '.pdf', $filetarget);
   $objWriter->save($fileresult);

   $toPdf = FileHelper::normalizePath($fileresult);

this is part of result after converted from docx to pdf

enter image description here

and this is part of origin docx file

enter image description here

what's wrong with my code?

chiper4
  • 303
  • 5
  • 18

1 Answers1

1

Unfortunately phpWord is very basic so for DocX to PDF output you can see there is no ability to preserve text or page breaks, nor support lists or export images.

For the current list of features see

https://phpword.readthedocs.io/en/latest/intro.html#writers

Since it runs OpenOffice as the converter you could try other PHP methods to run the conversion direct

K J
  • 8,045
  • 3
  • 14
  • 36