2010-05-26 41 views
11

Tôi cần thêm hình mờ vào một tệp PDF hiện có bằng cách sử dụng PHP. Tôi đã tìm kiếm trên Google cho nó, nhưng không thể tìm thấy bất kỳ thư viện phù hợp.Làm cách nào để thêm hình mờ vào tệp PDF hiện có bằng PHP?

Tôi đã tìm thấy thư viện fpdf tạo hình thu nhỏ xem trước của tệp PDF, nhưng tôi không biết liệu nó có thêm hình mờ vào tệp PDF hiện có hay không. Bất cứ ai có thể đề xuất một thư viện PHP hơn có thể hiển thị xem trước và thêm watermarks cho các tập tin PDF hiện có?

Trả lời

1

nhận được sự trợ giúp của Đánh dấu ở đây, chúng tôi đi http://www.fpdf.de/downloads/addons/9/ nếu các bạn nghĩ có, tôi sẽ đánh dấu câu trả lời của tôi là người chiến thắng.

Cảm ơn Jyelton cho câu trả lời cho câu hỏi của tôi trông giống như dòng chảy stackover không hoạt động ....

9

Chỉ cần một ví dụ quick'n'dirty sử dụng FPDF và lớp FPDI:

function PlaceWatermark($file, $text, $xxx, $yyy, $op, $outdir) { 
    require_once('fpdf.php'); 
    require_once('fpdi.php'); 
    $name = uniqid(); 
    $font_size = 5; 
    $ts=explode("\n",$text); 
    $width=0; 
    foreach ($ts as $k=>$string) { 
     $width=max($width,strlen($string)); 
    } 
    $width = imagefontwidth($font_size)*$width; 
    $height = imagefontheight($font_size)*count($ts); 
    $el=imagefontheight($font_size); 
    $em=imagefontwidth($font_size); 
    $img = imagecreatetruecolor($width,$height); 
    // Background color 
    $bg = imagecolorallocate($img, 255, 255, 255); 
    imagefilledrectangle($img, 0, 0,$width ,$height , $bg); 
    // Font color 
    $color = imagecolorallocate($img, 0, 0, 0); 
    foreach ($ts as $k=>$string) { 
     $len = strlen($string); 
     $ypos = 0; 
     for($i=0;$i<$len;$i++){ 
      $xpos = $i * $em; 
      $ypos = $k * $el; 
      imagechar($img, $font_size, $xpos, $ypos, $string, $color); 
      $string = substr($string, 1);  
     } 
    } 
    imagecolortransparent($img, $bg); 
    $blank = imagecreatetruecolor($width, $height); 
    $tbg = imagecolorallocate($blank, 255, 255, 255); 
    imagefilledrectangle($blank, 0, 0,$width ,$height , $tbg); 
    imagecolortransparent($blank, $tbg); 
    if (($op < 0) OR ($op >100)){ 
     $op = 100; 
    } 
    imagecopymerge($blank, $img, 0, 0, 0, 0, $width, $height, $op); 
    imagepng($blank,$name.".png"); 
    // Created Watermark Image 
    $pdf = new FPDI(); 
    if (file_exists("./".$file)){ 
     $pagecount = $pdf->setSourceFile($file); 
    } else { 
     return FALSE; 
    } 
    $tpl = $pdf->importPage(1); 
    $pdf->addPage(); 
    $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE); 
    //Put the watermark 
    $pdf->Image($name.'.png', $xxx, $yyy, 0, 0, 'png'); 
    if ($outdir === TRUE){ 
     return $pdf->Output(); 
    } else { 
     return $pdf; 
    } 
} 

PlaceWatermark("filename.pdf", "This is a lazy, but still simple test\n This should stand on a new line!", 30, 120, 100,TRUE); 

Cách sử dụng: PlaceWatermark($filename, $text, $x, $y, $opacity, $directoutput);

$filename - đường dẫn của file PDF mà bạn muốn đặt Watermark
$text - các văn bản Watermark bạn muốn thêm
$x - tọa độ x, nơi bạn muốn đặt Watermark
$y - tọa độ y nơi bạn muốn đặt Watermark
$opacity - Opacity của văn bản
$directoutput - Nếu chức năng TRUE sẽ ra một tập tin PDF, ngược lại nó sẽ trả lại $ pdf
Như tôi đã nói, đây là một ví dụ rất nhanh và bẩn, cần một số cải tiến.

+1

Mã chỉ hoạt động trên một trang. Làm cách nào để thay đổi mã để hỗ trợ nhiều trang nhập liệu hơn? – jonasl

+0

Chỉ trang 1. Để sửa đổi thay đổi $ tpl = $ pdf-> importPage (1); $ pdf-> addPage(); $ pdf-> useTemplate ($ tpl, 1, 1, 0, 0, TRUE); –

5

Đối với bất cứ ai khác mà tình cờ gặp bài này, bạn có thể tạo ra nhiều trang sử dụng một vòng lặp for

for($i=1; $i <= $pagecount; $i++) { 
    $tpl = $pdf->importPage($i);  
    $pdf->addPage();  
    $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE);  
    //Put the watermark 
    $pdf->Image($name.'.png', $xxx, $yyy, 0, 0, 'png');} 
+1

Đó là một liên kết chết. –

0
/* index.php */ 
    require('rotation.php'); 

    class PDF extends PDF_Rotate{ 
      protected $_outerText1;// dynamic text 
     protected $_outerText2; 

     function setWaterText($txt1="", $txt2=""){ 
      $this->_outerText1 = $txt1; 
      $this->_outerText2 = $txt2; 
     } 

     function Header(){ 
      //Put the watermark 
      $this->SetFont('Arial','B',40); 
      $this->SetTextColor(255,192,203); 
        $this->SetAlpha(0.5); 
      $this->RotatedText(35,190, $this->_outerText1, 45); 
      $this->RotatedText(75,190, $this->_outerText2, 45); 
     } 

     function RotatedText($x, $y, $txt, $angle){ 
      //Text rotated around its origin 
      $this->Rotate($angle,$x,$y); 
      $this->Text($x,$y,$txt); 
      $this->Rotate(0); 
     } 
    } 

    $file = "path/filename.pdf";// path: file name 
    $pdf = new PDF(); 

    if (file_exists($file)){ 
     $pagecount = $pdf->setSourceFile($file); 
    } else { 
     return FALSE; 
    } 

    $pdf->setWaterText("w a t e r M a r k d e m o ", "s e c o n d L i n e o f t e x t"); 

    /* loop for multipage pdf */ 
    for($i=1; $i <= $pagecount; $i++) { 
    $tpl = $pdf->importPage($i);    
    $pdf->addPage(); 
    $pdf->useTemplate($tpl, 1, 1, 0, 0, TRUE); 
    } 
    $pdf->Output(); //specify path filename to save or keep as it is to view in browser 

/* rotation.php */ 
    require('fpdf.php'); 
require('fpdi.php'); 
class PDF_Rotate extends FPDI 
{ 
    var $angle=0; 
    var $extgstates = array(); 

    function Rotate($angle,$x=-1,$y=-1) 
    { 
     if($x==-1) 
      $x=$this->x; 
     if($y==-1) 
      $y=$this->y; 
     if($this->angle!=0) 
      $this->_out('Q'); 
     $this->angle=$angle; 
     if($angle!=0) 
     { 
      $angle*=M_PI/180; 
      $c=cos($angle); 
      $s=sin($angle); 
      $cx=$x*$this->k; 
      $cy=($this->h-$y)*$this->k; 
      $this->_out(sprintf('q %.5F %.5F %.5F %.5F %.2F %.2F cm 1 0 0 1 %.2F %.2F cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy)); 
     } 
    } 

    function _endpage() 
    { 
     if($this->angle!=0) 
     { 
      $this->angle=0; 
      $this->_out('Q'); 
     } 
     parent::_endpage(); 
    } 

    function SetAlpha($alpha, $bm='Normal') 
    { 
     // set alpha for stroking (CA) and non-stroking (ca) operations 
     $gs = $this->AddExtGState(array('ca'=>$alpha, 'CA'=>$alpha, 'BM'=>'/'.$bm)); 
     $this->SetExtGState($gs); 
    } 

    function AddExtGState($parms) 
    { 
     $n = count($this->extgstates)+1; 
     $this->extgstates[$n]['parms'] = $parms; 
     return $n; 
    } 

    function SetExtGState($gs) 
    { 
     $this->_out(sprintf('/GS%d gs', $gs)); 
    } 

    function _enddoc() 
    { 
     if(!empty($this->extgstates) && $this->PDFVersion<'1.4') 
      $this->PDFVersion='1.4'; 
     parent::_enddoc(); 
    } 

    function _putextgstates() 
    { 
     for ($i = 1; $i <= count($this->extgstates); $i++) 
     { 
      $this->_newobj(); 
      $this->extgstates[$i]['n'] = $this->n; 
      $this->_out('<</Type /ExtGState'); 
      foreach ($this->extgstates[$i]['parms'] as $k=>$v) 
       $this->_out('/'.$k.' '.$v); 
      $this->_out('>>'); 
      $this->_out('endobj'); 
     } 
    } 

    function _putresourcedict() 
    { 
     parent::_putresourcedict(); 
     $this->_out('/ExtGState <<'); 
     foreach($this->extgstates as $k=>$extgstate) 
      $this->_out('/GS'.$k.' '.$extgstate['n'].' 0 R'); 
     $this->_out('>>'); 
    } 

    function _putresources() 
    { 
     $this->_putextgstates(); 
     parent::_putresources(); 
    } 

} 

thử kịch bản this.modified từ FPDF dụ kịch bản. Chỉnh sửa: Đã thêm độ mờ sử dụng $ this-> SetAlpha (0.5)

Các vấn đề liên quan