hulkyuan
hulkyuan

Reputation: 71

Import page from external PDF file can't display properly

The PDF file have a large width than normal.

$pagecount = $mpdf->SetSourceFile($pdfurl);
for($x=0;$x<$pagecount;$x++){
   $tplId = $mpdf->ImportPage(($x+1));
    $mpdf->UseTemplate($tplId);
    $mpdf->AddPage();
}

Most PDF is displayed well, but with large width, it only display a part of it.

Upvotes: 0

Views: 1238

Answers (2)

hulkyuan
hulkyuan

Reputation: 71

Thanks @Jan.Your code not work perfectly for me.But point me the right direction.

for($pageNo = 1; $pageNo <= $pageCount ; $pageNo++){
    $tplId = $mpdf->ImportPage($pageNo);
    $size = $mpdf->GetTemplateSize($tplId);
    $w=210;
    if($size['w'] > $size['h']){
        $w=299;
    }
    $mpdf->AddPageByArray([
    'orientation' => $size['w'] > $size['h'] ? 'L' : 'P'
    ]);
    $mpdf->UseTemplate($tplId,0,0,$w);

}

But my solution got a problem.The landscape mode will scale the PDF size a little small.

Upvotes: 0

Jan Slabon
Jan Slabon

Reputation: 5058

There are several issues in your code:

  1. You call UseTemplate() before you have added a page.
  2. The AddPage() method adds a page in the default page size, which was defined in the constructor.

So you have to change your script to:

$pageCount = $mpdf->SetSourceFile($pdfurl);
for($pageNo = 1; $pageNo <= $pageCount ; $pageNo++){
    $tplId = $mpdf->ImportPage($pageNo);
    $size = $mpdf->GetTemplateSize($pageNo);
    $mpdf->AddPageByArray([
        'orientation' => $size['w'] > $size['h'] ? 'L' : 'P',
        'newformat' => [$size['w'], $size['h']]
    ]);
    $mpdf->UseTemplate($tplId);
}

Upvotes: 1

Related Questions