Yii2 mPdf Kartik: Set Height and Width Paper

2019-07-30 00:39发布

问题:

How to set custom height and width on PDF format like this:

I want set height: 13.6cm and width: 21.2cm

if I export to PDF, paper size is always in A4 format, how to make paper size to be 13.6cm (height) and **21.2cm (width) as in the picture above

I use mpdf kartik on yii2 ..

this my controller code:

$pdf = new Pdf([
                'mode' => Pdf::MODE_UTF8,
                'format' => Pdf::FORMAT_A4,
                'destination' => Pdf::DEST_BROWSER,
                'content' => $content,
                'cssFile' => \Yii::getAlias('@webroot') .'/css/pdf/pdf-pfi.css',
                'marginLeft' => 5, 'marginTop' => 5, 'marginRight' => 5, 'marginBottom' => 5,
                'defaultFont' => 'Calibri',
            ]);
            return $pdf->render();

hopefully it can be helped ...

回答1:

The format can be specified either as a pre-defined page size, or as an array of width and height in millimetres.

Use like

$pdf = new Pdf([
    'mode' => Pdf::MODE_UTF8,
    'format' => [212, 136], // here define custom [width, height] in mm
    // 'format' => Pdf::FORMAT_A4,
    'destination' => Pdf::DEST_BROWSER,
    'content' => $content,
    'cssFile' => \Yii::getAlias('@webroot') .'/css/pdf/pdf-pfi.css',
    'marginLeft' => 5, 
    'marginTop' => 5, 
    'marginRight' => 5, 
    'marginBottom' => 5,
    'defaultFont' => 'Calibri',
]);
return $pdf->render();

Refer kartik-v/yii2-mpdf or mPdf