4

因此,我使用fpdi1.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;
}

一切都很好,除了一个小问题:当我有一个第一页横向的文档时,生成的文档中的所有页面都从底部和右侧裁剪(如果第一页是纵向模式,一切都很好,即使后续页面处于横向模式)。问题很明显:这个函数有什么问题?

4

1 回答 1

13

最后一个参数useTemplate()指定是否调整页面大小。它默认为false,但我们希望它是true

更改此调用:

$pdf->useTemplate($tpl);

至(对于较旧的 fpdf 版本):

$pdf->useTemplate($tpl, null, null, $size['w'], $size['h'], true);

或(对于较新的 fpdf 版本):

$pdf->useTemplate($tpl, null, null, $size['width'], $size['height'], true);

Consider updating all fpdf-related libraries (fpdf, fpdf_tpl and fpdi) to the most recent versions - that's important, too.

P.S.: When pushing new version of fpdi on testing server, I found that it doesn't work with relatively old versions of PHP interpreter - it does work with version 5.3.10, but it can't work with 5.3.2 or older. So, make sure that you have an up-to-date PHP version too.

于 2012-09-18T09:32:16.680 回答