为什么fpdi裁剪pdf页面?

时间:2012-09-18 06:57:23

标签: php fpdf fpdi

所以,我正在使用fpdi版本1.2为我的文档中的每个页面添加一些小标记。这是我的代码:

public static function markPdf($file, $text){
    define('FPDF_FONTPATH','font/');
    require_once('fpdi/fpdf.php');
    require_once('fpdi/fpdi.php');
    try{
        $pdf = new FPDI();
        $pagecount = $pdf->setSourceFile($file);
        for($i = 1 ; $i <= $pagecount ; $i++){
            $tpl  = $pdf->importPage($i);
            $size = $pdf->getTemplateSize($tpl);

            // Here you can see that I'm setting orientation for every single page,
            // depending on the orientation of the original page
            $orientation = $size['h'] > $size['w'] ? 'P':'L';
            $pdf->AddPage($orientation);
            $pdf->useTemplate($tpl);

            $pdf->SetXY(5, 5);
            $pdf->SetTextColor(150);
            $pdf->SetFont('Arial','',8);
            $pdf->Cell(0,0,$text,0,1,'R');
        }
        $pdf->Output($file, "F");
    }catch(Exception $e){
        Logger::log("Exception during marking occurred");
        return false;
    }
    return true;
}

一切正常,除了一个小问题:当我有横向第一页的文档时,生成的文档中的所有页面都从底部和右侧裁剪(如果第一页处于纵向模式,一切即使后续页面处于横向模式,也可以正常运行。 问题很明显:这个功能出了什么问题?

1 个答案:

答案 0 :(得分:12)

好的,最后我解决了这个问题。我改变了呼叫
 $pdf->useTemplate($tpl);

$pdf->useTemplate($tpl, null, null, $size['w'], $size['h'], true);
(最重要的是最后一个参数$adjustPageSize,默认情况下它的值为false) 我还将所有fpdf相关的库(fpdffpdf_tplfpdi)更新到最新版本 - 这也很重要。希望它对某人有用。

PS:在测试服务器上推送fpdi的新版本时,我发现它不适用于相对较旧版本的php解释器 - 它确实适用于5.3.10版本,但它无法正常工作5.3.2或更早。因此,请确保您拥有最新的PHP版本。

相关问题