2012-09-18 57 views
4

因此,我使用fpdi版本1.2为我的文档中的每个页面添加一些小标记。这里是我的代码:为什么fpdi会裁剪pdf页面?

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

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

回答

12

好的,最后我解决了这个问题。我换了电话
$pdf->useTemplate($tpl);

$pdf->useTemplate($tpl, null, null, $size['w'], $size['h'], true);
(最重要的是最后一个参数,$adjustPageSize,默认情况下它拥有的false值)。
而且我还将所有fpdf相关库(fpdf,fpdf_tplfpdi)更新为最新版本 - 这一点也很重要。希望它对某人有用。 PS:当在测试服务器上推送fpdi的新版本时,我发现它不适用于相对较旧版本的PHP解释器 - 它可以与5.3.10版本一起工作,但它不能与5.3一起使用。 2岁或以上。所以,确保你有一个最新的PHP版本。

+0

谢谢你。这帮助了我很多,你不知道。 – bryan

+0

@bryan不用客气。 :) – aga

+0

当我使用这段代码时,它在PDF的右侧和底部添加了非常大的空白,你能帮助我吗?我在一个文档中有不同大小页面的PDF – RPDeshaies