gpt4 book ai didi

php - 为什么 fpdi 会裁剪 pdf 页面?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:59:31 28 4
gpt4 key购买 nike

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

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

最佳答案

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)更新到最新版本- 这也很重要。

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

关于php - 为什么 fpdi 会裁剪 pdf 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471842/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com