Converting docx to PDF using PHPWord changing layout, borders,

Viewed 292

I'm using PHPOffice/PHPWord to populate a template file using TemplateProcessor and saving it. This works fine and the resulting docx file is fine.

Then I am wanting to simply convert the docx into a PDF for output to the user.

I've tried

//...
$filename = 'test';
if(YII_ENV_DEV){
    $filepath = Yii::$app->basePath.'\web\files\excel\\';
}else{
    $filepath = \Yii::getAlias('@webroot').'/files/excel/';
}
$fileWord = $filepath . $filename . '.docx';
$templateProcessor->saveAs($fileWord);

Settings::setPdfRendererPath(\Yii::getAlias('@vendor')."/mpdf/mpdf");
Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF);
$phpWord = \PhpOffice\PhpWord\IOFactory::load($fileWord);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;filename="'.$filename.'.pdf"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

which does result in a PDF, but it throws all the table formatting out the window, merged cells are no longer merged, tables with no borders (used for layout) now have borders, border width isn't respected, ...

I have tried using all 3 different PDF Renderers: TCPDF, DOMPDF, MPDF and always issues.

How can I simply output the file as a PDF as it is in the docx?

This is on a shared server that I have no control over.

0 Answers
Related