问题描述
我修改了以下堆栈问题:当用户尝试下载文件时在PDF文件上应用水印,但我遇到了一个错误,尽管有一条评论说如何修复它,但不够详尽.
I modified this stack question: Applying watermarks on pdf files when users try to download the files but I encountered an error, though there was a comment that says on how to fix it, it wasn't elaborate enough.
这是代码:
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时,所有页面合并到一个页面中.我在这篇文章中附上了图片.
When I load a pdf that has more than 2 all of the pages merges in one page. I attached the image in this post.
谢谢.
推荐答案
我发现该脚本有几处错误.要使其正常工作,请将doWatermark()
方法更改为:-
I have found a couple of things wrong with that script. To get it working change the doWatermark()
method to this:-
public function doWaterMark()
{
$currentFile = $this->file;
$newFile = $this->newFile;
$pagecount = $this->pdf->setSourceFile($currentFile);
for($i = 1; $i <= $pagecount; $i++){
$this->pdf->addPage();//<- moved from outside loop
$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->_rotate(0);//<-added
}
$this->pdf->Output($newFile, 'F');
}
我将行$this->pdf->addPage();
移到了循环中,否则所有内容都输出到一页上.我还添加了$this->_rotate(0);
以便在保存文档之前使文档恢复直立状态.真的很简单.我已经为您评论了更改的行.
I moved the line $this->pdf->addPage();
into the loop, as otherwise everything is output onto one page. I also added $this->_rotate(0);
to bring the document back upright before saving it out. Pretty simple really. I have commented the changed lines for you.
我在32页的pdf纸上进行了测试,效果似乎很好.
I tested it on a 32 page pdf and it seemed to work fine.
这篇关于FPDI/FPDF:水印和打印多页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!