phppdfdompdflaravel-5.7phpword

Greek letters are replaced with ??? once I convert a docx into pdf using PHPWord?


I made the following simple laravel artisan command:

Artisan::command('test:pdf',function(){

    \PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_DOMPDF);
    \PhpOffice\PhpWord\Settings::setPdfRendererPath('.');

    $tmpFile = storage_path().'/contracts/test.docx';
    $outfile = storage_path().'/contracts/test.pdf';

    $phpWord = \PhpOffice\PhpWord\IOFactory::load($tmpFile);
    $phpWord->save($outfile,'PDF');

});

It receives a docx file and converts it into pdf. But for an ackward reason the docx contains greek letters and once rendered in pdf are converted into ????.

So any ideas how I can save a pdf file from a docx respecting UTF-8 characters?


Solution

  • This is caused by DOMPDF's constraints that is unable to render the font for Greek Language.

    You can either replace all fonts to DejaVu ones OR use mpdf.

    Artisan::command('test:pdf',function(){
    
        \PhpOffice\PhpWord\Settings::setPdfRendererName(\PhpOffice\PhpWord\Settings::PDF_RENDERER_MPDF);
        \PhpOffice\PhpWord\Settings::setPdfRendererPath('.');
    
        $tmpFile = storage_path().'/contracts/test.docx';
        $outfile = storage_path().'/contracts/test.pdf';
    
        $phpWord = \PhpOffice\PhpWord\IOFactory::load($tmpFile);
        $phpWord->save($outfile,'PDF');
    
    });