php打印分页的水印,php – FPDI / FPDF:水印和打印多页

我修改了这个堆栈问题:

Applying watermarks on pdf files when users try to download the files但是我遇到了一个错误,虽然有一条评论说如何解决它,但它还不够详细.

这是代码:

require_once('fpdf/fpdf.php');

require_once('fpdi/fpdi.php');

class WaterMark

{

public $pdf, $file, $newFile,

$wmText = "STACKOVERFLOW";

/** $file and $newFile have to include the full path. */

public function __construct($file, $newFile)

{

$this->pdf = new FPDI();

$this->file = $file;

$this->newFile = $newFile;

}

/** $file and $newFile have to include the full path. */

public static function applyAndSpit($file, $newFile)

{

$wm = new WaterMark($file, $newFile);

if($wm->isWaterMarked())

return $wm->spitWaterMarked();

else{

$wm->doWaterMark();

return $wm->spitWaterMarked();

}

}

/** @todo Make the text nicer and add to all pages */

public function doWaterMark()

{

$currentFile = $this->file;

$newFile = $this->newFile;

$this->pdf->addPage();

$pagecount = $this->pdf->setSourceFile($currentFile);

for($i = 1; $i <= $pagecount; $i++){

$tplidx = $this->pdf->importPage($i);

$this->pdf->useTemplate($tplidx, 10, 10, 100);

// now write some text above the imported page

$this->pdf->SetFont('Arial', 'I', 40);

$this->pdf->SetTextColor(255,0,0);

$this->pdf->SetXY(25, 135);

$this->_rotate(55);

$this->pdf->Write(0, $this->wmText);

}

$this->pdf->Output($newFile, 'F');

}

public function isWaterMarked()

{

return (file_exists($this->newFile));

}

public function spitWaterMarked()

{

return readfile($this->newFile);

}

protected function _rotate($angle,$x=-1,$y=-1) {

if($x==-1)

$x=$this->pdf->x;

if($y==-1)

$y=$this->pdf->y;

if($this->pdf->angle!=0)

$this->pdf->_out('Q');

$this->pdf->angle=$angle;

if($angle!=0){

$angle*=M_PI/180;

$c=cos($angle);

$s=sin($angle);

$cx=$x*$this->pdf->k;

$cy=($this->pdf->h-$y)*$this->pdf->k;

$this->pdf->_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));

}

}

}

header('Content-type: application/pdf');

//header('Content-Disposition: attachment; filename="downloaded.pdf"');

WaterMark::applyAndSpit('C:\xampp\htdocs\tst\test0.pdf','C:\xampp\htdocs\tst\output0.pdf');

当我加载一个超过2的pdf时,所有页面合并在一个页面中.我在这篇文章中附上了图片.

谢谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值