Php 为什么fpdi裁剪pdf页面?
因此,我正在使用Php 为什么fpdi裁剪pdf页面?,php,fpdf,fpdi,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
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;
}
公共静态函数markPdf($file,$text){
定义('FPDF_FONTPATH','font/');
一次需要_('fpdi/fpdf.php');
一次需要_('fpdi/fpdi.php');
试一试{
$pdf=新的FPDI();
$pagecount=$pdf->setSourceFile($file);
对于($i=1;$i进口页($i);
$size=$pdf->getTemplateSize($tpl);
//在这里你可以看到,我正在为每一页设置方向,
//取决于原始页面的方向
$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->输出($file,“F”);
}捕获(例外$e){
Logger::log(“标记期间发生异常”);
返回false;
}
返回true;
}
除了一个小问题外,一切都正常:当我有一个文档的第一页是横向的,生成的文档中的所有页面都是从底部和右侧裁剪的(如果第一页是纵向模式,则一切都正常,即使后面的页面是横向模式)。
问题很明显:此函数有什么问题?useTemplate()的最后一个参数指定是否调整页面大小。它默认为
false
,但我们希望它为true
更改此呼叫:
$pdf->useTemplate($tpl);
至(对于较旧的fpdf版本):
或(对于较新的fpdf版本):
考虑将所有与fpdf
相关的库(fpdf
、fpdf\u tpl
和fpdi
)更新到最新版本,这也很重要
注:在测试服务器上推出新版本的
fpdi
时,我发现它不适用于相对较旧的PHP解释器版本-它适用于5.3.10版本,但不能适用于5.3.2或更旧的版本。因此,请确保您也有最新的PHP版本。谢谢您。这对我帮助很大,您根本不知道。当我使用此代码时,它在PDF的右侧和底部添加了非常大的边距,你能帮我吗?我在一个文档中有不同大小的页面的PDF谢谢,它节省了很多时间。哇,我完全没想到我的答案在6-7年后仍然相关
$pdf->useTemplate($tpl, null, null, $size['w'], $size['h'], true);
$pdf->useTemplate($tpl, null, null, $size['width'], $size['height'], true);