0

If you know smth about this theme please leave a comment. I am trying to get pdf file from docx using phpoffice/word. But i cant make it real because for whole day testing i get only this error:

PHP Fatal error:  Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'PDF rendering library or library path has not been defined.

Please don't tell me to look this question in www, i tried - nothing helpful. My code:

require './vendor/autoload.php';
require './vendor/phpoffice/phpword/bootstrap.php';
$filename = 'example';
echo PHPWORD_BASE_DIR . '/TCPDF-master';
$wordPdf = \PhpOffice\PhpWord\IOFactory::load("./file_to_fill/ДКП квартира физики.docx");
$rendererName = \PhpOffice\PhpWord\Settings::PDF_RENDERER_TCPDF;
$rendererLibrary = 'TCPDF';
$rendererLibraryPath = dirname(__FILE__).'/'. $rendererLibrary;
\PhpOffice\PhpWord\Settings::setPdfRendererPath($rendererLibraryPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');

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

I know that my problem is library, but i really can't understand where the file of this library is. May be it happens because today for the first time i was trying to work with composer. I tried different library like dompdf and TCPDF and my be some others. So if you have any ideas welcome.=)

Kirill
  • 11
  • 2
  • 4

1 Answers1

0

So after many hours of searching and testing i got the working code. May be i am not only one need this. For TCPDF library:

define('PHPWORD_BASE_DIR', realpath(__DIR__));    
require './vendor/autoload.php';
require './TCPDF-master/tcpdf.php';
$PdfPath = realpath(PHPWORD_BASE_DIR . '/TCPDF-master');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($PdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$phpWord = \PhpOffice\PhpWord\IOFactory::load('yourtemplate.docx');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');

For mpdf library:

define('PHPWORD_BASE_DIR', realpath(__DIR__));
require './vendor/autoload.php';
require './vendor/mpdf/mpdf/mpdf.php';
$PdfPath = realpath(PHPWORD_BASE_DIR . '/vendor/mpdf/mpdf');
\PhpOffice\PhpWord\Settings::setPdfRendererPath($PdfPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('MPDF');
$phpWord = \PhpOffice\PhpWord\IOFactory::load('temp.docx');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save('result.pdf');  

But the the domPDF doesn't work at all( So this two libraries works but not good, one of it gives me "???" when i input the Russian text. And both of them gives a lot of different free space in document (may be margin). And i can't understand how edit it.

Kirill
  • 11
  • 2
  • 4