回答
尽管PHP有几个非常好的PDF库,但如果我正在编写这样的程序,我只需要运行pdftk即可,但仍需要生成水印。
$tempfile=tempnam();
system("pdftk input_file.pdf background watermark.pdf output $tempfile dont_ask", $errcode);
if (!$errcode && $ih=fopen($tempfile, 'r')) {
header('Content-Type: application/pdf');
fpassthru($ih);
fclose($ih);
} else {
print "Whoops";
}
unlink($tempfile);
有一个很好的开源php库http://www.tcpdf.org/,我用它来处理所有的pdf生成任务。
需要做到这一点昨天这里是如何没有任何外部非PHP库需要安装。只需要两个库就是两个PHP库,并且很容易获得。
- FPDF - >http://www.fpdf.org/最新版本
- FPDI - > (确保抛fpdf_tpl.php 文件在同一文件夹中fpdi.php)
现在你可以使用下面的类来实现水印
/** MAKE SURE TO HAVE THE INCLUDES RUNNING PROPERLY */
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;
$pagecount = $this->pdf->setSourceFile($currentFile);
for($i = 1; $i <= $pagecount; $i++){
$this->pdf->addPage();
$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);
}
$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));
}
}
}
现在,您可以运行此为:
WaterMark::applyAndSpit($fileWithFullPath);
我用这个,并获得所有页面显示在第一页。我可以通过在'doWaterMark()'的循环结尾添加'$ this-> pdf-> addPage();来解决这个问题。我做了一些其他小改动,所以我没有信心编辑你的答案,但如果其他人有这个问题,这可能会有所帮助。 – 2012-04-11 15:43:26
我更正了此代码以回应[此问题](http://stackoverflow.com/q/10468478/212940) – vascowhite 2012-05-08 06:33:16
感谢您的代码。到目前为止,我发现了几件小事,使它不再是即插即用的。 “运行这个”命令需要两个参数:WaterMark :: applyAndSpit($ fileWithFullPath,$ newfilenameWithFullPath); ,并且该类使用=&,在当前的php中已弃用,只是使用=。 – rpilkey 2015-11-05 14:05:57
- 1. 水印PDF文件?
- 2. 尝试下载文件
- 3. 尝试使用Symfony2下载并保存PDF文件
- 4. 用JavaScript下载PDF文件
- 5. 用C#下载PDF文件
- 6. 当我尝试使用我的应用下载文件时,无法写入文件
- 7. 当尝试下载文件时出现错误消息
- 8. 当我尝试下载文件时出现错误
- 9. 如何在用户尝试下载文件时显示进度条
- 10. Ajax调用在jsp下载pdf文件
- 11. 尝试使用Python下载文件时出错
- 12. 尝试使用Python下载文件时出现语法错误
- 13. Amazon S3文件读取超时。尝试使用JAVA下载文件
- 14. PHP:试图使pdf文件可下载
- 15. vb.net - WebBrowser1.Navigate尝试下载JSON文件
- 16. Internet Explorer尝试下载index.jsf文件
- 17. 当尝试使用获取AttributeError的Python FTP_TLS对象下载文件时?
- 18. 下载.php文件,而不是.pdf文件,当我点击下载在PHP
- 19. 当试图下载apk文件时,文件不喜欢异常
- 20. 尝试使用jsoup将URL事件下载到.txt文件中
- 21. 从Zip文件中下载PDF文件
- 22. 使用水壶从FTP下载文件
- 23. 从ajax响应下载pdf文件
- 24. 使用CMIS在文档上设置水印查看或下载
- 25. 使用php下载pdf文件
- 26. 如何使用asp.net下载pdf文件?
- 27. 使用chromedriver直接下载PDF文件
- 28. 使用webcrawler下载pdf文件
- 29. 使用OKHTTP下载pdf文件
- 30. 使用htaccess强制PDF文件下载
他们的下载脚本可能会抓住你的用户名,创建基本图像,并使用'pdf_php'库层上的PDF顶部的形象。 – drudge 2010-10-21 00:49:50
您是如何生成(或计划)PDF的?如果你还没有做任何事情,看看Webkit到PDF:http://code.google.com/p/wkhtmltopdf/ – Petah 2010-10-21 01:29:22