【问题标题】:Why does fpdi crop the pdf page?为什么 fpdi 会裁剪 pdf 页面?
【发布时间】:2012-09-18 06:57:22
【问题描述】:

所以,我使用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;
}

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

【问题讨论】:

    标签: php fpdf fpdi


    【解决方案1】:

    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);
    

    考虑将所有与 fpdf 相关的库(fpdffpdf_tplfpdi)更新到最新版本 - 这也很重要。

    PS:在测试服务器上推送fpdi的新版本时,我发现它不适用于相对旧版本的PHP解释器 - 它适用于版本5.3.10,但不适用于5.3 .2 或更早。因此,请确保您也拥有最新的 PHP 版本。

    【讨论】:

    • 谢谢你。这对我帮助很大,你不知道。
    • 当我使用此代码时,它会在 PDF 的右侧和底部添加非常大的边距,您能帮我解决这个问题吗?我的 PDF 在一个文档中的页面大小不同
    • 谢谢,它节省了很多时间。
    • 哇,我完全没想到我的答案在 6-7 年后仍然有意义。 :)
    • @aga 10 年,依然强劲
    猜你喜欢
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2010-10-02
    • 2014-12-19
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多