2012-03-14 71 views
0

有谁知道如何以编程方式向PHP中的PDF文件添加覆盖图或图章?到目前为止,我所提出的最好的方法是使用exec命令调用PDFTK库。如果存在的话,我更喜欢实际的PHP方法。如何在PHP中为PDF文件添加叠加/戳记?

+2

这有帮助吗? http://stackoverflow.com/questions/2913934/how-i-can-add-watermark-to-existing-pdf-file-using-php – wanovak 2012-03-14 15:39:59

+0

如果您可以承担许可费用,[PDFlib](http:// pdflib.com)非常好。 – 2012-03-14 15:42:14

+0

我看着那个帖子wanovak,但无法弄清楚。现在我仔细看了看,这对我有用。谢谢! – dmikester1 2012-03-14 16:02:27

回答

3

这是一个老问题,但我希望能帮助某人寻找这个答案。

我已经成功地使用了免费的PHP库FPDF(http://www.fpdf.org)。

我已经从一个大功能中删除了这段代码,请注意我在这里不包含的缺失变量。

 $pdf = new FPDI(); 

     // Number of pages of the PDF 
     $pagecount = $pdf->setSourceFile($source."/".$pdfList[$i]); 

     // Loop the PDF's pages 
     for($page_index=0; $page_index < $pagecount; $page_index++) 
     { 

      $tplidx = $pdf->importPage(($page_index+1), '/MediaBox'); 
      $pdf->addPage(); 
      $pdf->useTemplate($tplidx); 

      // Do I need to stamp this page? This is a boolean flag calculated from the settings for each page. 
      $stampThis = false; 

      if($settings->pages == 'all') 
      { 
       $stampThis = true; 
      } 
      else if($settings->pages == 'last') 
      { 
       if($pagecount == $page_index+1) 
       { 
        $stampThis = true; 
       } 
      } 
      else if($settings->pages == 'first') 
      { 
       if($page_index == 0) 
       { 
        $stampThis = true; 
       } 
      } 
      else if($settings->pages == 'odd') 
      { 
       if($page_index%2 == 0) 
       { 
        $stampThis = true; 
       } 
      } 
      else if($settings->pages == 'even') 
      { 
       if($page_index%2 != 0) 
       { 
        $stampThis = true; 
       } 
      }   

      // Stamp the PDF, in case the flag is true 
      if($stampThis) 
      { 
       // Custom stamp 
       if(!empty($settings->imageURL)) { 

        $pdf->Image($settings->imageURL, 
           $settings->stamp_coord_x, 
           $settings->stamp_coord_y); 
       } 

      } // stamp if end 

     } // pages loop end 

     $pdf->Output($destination.'/'.$pdfList[$i], 'F'); 
+0

谢谢!不再在该项目上工作,所以我无法测试。但它对我来说很好! – dmikester1 2014-08-11 14:29:49