php pdf 文字水印图片,php如何给pdf加上文字水印和图片水印[未测试]

php给pdf加上水印

环境

php5.5.12

fpdi-1.5.2

fpdf-1.7

原理

利用fpdi来加载已知pdf文件,用fpdf对pdf进行操作

注意事项

免费的fpdi只支持处理pdf1.4及以下版本,1.5以上就需要用到FPDI PDF-Parser插件

使用方法

fpdi-1.5.2

fpdf-1.7

1.文字水印 word.php

require_once('./fpdf/fpdf.php');

require_once('./fpdi/fpdi.php');

//word_watermark

$pdf = new FPDI();

// get the page count

$pageCount = $pdf->setSourceFile('more.pdf');

// iterate through all pages

for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)

{

// import a page

$templateId = $pdf->importPage($pageNo);

// get the size of the imported page

$size = $pdf->getTemplateSize($templateId);

// create a page (landscape or portrait depending on the imported page size)

if ($size['w'] > $size['h']) $pdf->AddPage('L', array($size['w'], $size['h']));

else $pdf->AddPage('P', array($size['w'], $size['h']));

// use the imported page

$pdf->useTemplate($templateId);

$pdf->SetFont('Arial','B','12');

// sign with current date

$pdf->SetXY(0, 0); // you should keep testing untill you find out correct x,y values

$pdf->Write(7, date('Y-m-d'));

}

$pdf->Output('word.pdf');

2.图片水印 pic.php

require_once('./fpdf/fpdf.php');

require_once('./fpdi/fpdi.php');

//pic_watermark

$pdf = new FPDI();

// get the page count

$pageCount = $pdf->setSourceFile('more.pdf');

// iterate through all pages

for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)

{

// import a page

$templateId = $pdf->importPage($pageNo);

// get the size of the imported page

$size = $pdf->getTemplateSize($templateId);

// create a page (landscape or portrait depending on the imported page size)

if ($size['w'] > $size['h']) $pdf->AddPage('L', array($size['w'], $size['h']));

else $pdf->AddPage('P', array($size['w'], $size['h']));

// use the imported page

$pdf->useTemplate($templateId);

// Place the graphics

$pdf->image("test.png", 75, 85, 50);

}

$pdf->Output('pic.pdf');

3.项目地址

pdf_watermark

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值