Search code examples
phppdfdocxdompdfphpword

is there some line of code in phpword to setup papersize in docx to pdf convertion


I'm trying to convert an existing docx file to pdf with domPDF and PHPWord, and everything works fine, but the resultant file is A4 size, and the original docx file is Letter, the resultant file must be letter too, what can I do? please help.

$existingfile='../../CC BB03-04-30.docx';

Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
Settings::setPdfRendererPath('../../dompdf-master/vendor/');

$phpWord = IOFactory::load($existingfile, 'Word2007');
$phpWord->save('../../test.pdf', 'PDF');

Like I said, the code works fine, the only issue that I'm trying to solve, is the paper size. I need letter size to the PDF file.


Solution

  • Try

    composer require phpoffice/phpword
    

    Php:

    Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
    Settings::setPdfRendererPath('../../dompdf-master/vendor/');
    
    $paper = new \PhpOffice\PhpWord\Style\Paper();
    $paper->setSize('Letter');  
    $phpWord = IOFactory::load($existingfile, 'Word2007');
    
    $phpWord->addSection(['pageSizeW' => $paper->getWidth(), 'pageSizeH' => $paper->getHeight()]);
    $phpWord->save('../../test.pdf', 'PDF');