当前位置: 移动技术网 > IT编程>开发语言>PHP > php如何给pdf加上文字水印和图片水印

php如何给pdf加上文字水印和图片水印

2017年12月23日  | 移动技术网IT编程  | 我要评论

搞笑二人转唐僧泡妞,汴京清谈,绿豆蛙打酱油

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

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

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');

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网