php如何給pdf加上文字水印和圖片水印[未測試]

php給pdf加上水印
環境
php5.5.12
fpdi-1.5.2
fpdf-1.7
原理
利用fpdi來加載已知pdf文件,用fpdf對pdf進行操做php

注意事項
免費的fpdi只支持處理pdf1.4及如下版本,1.5以上就須要用到FPDI PDF-Parser插件git

使用方法
fpdi-1.5.2
fpdf-1.7
1.文字水印 word.phpgithub

<?phpui

require_once('./fpdf/fpdf.php');
require_once('./fpdi/fpdi.php');插件


//word_watermark
$pdf = new FPDI();圖片

// get the page count
$pageCount = $pdf->setSourceFile('more.pdf');get

// iterate through all pages
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++)
{
    // import a page
    $templateId = $pdf->importPage($pageNo);it

    // get the size of the imported page
    $size = $pdf->getTemplateSize($templateId);pdf

    // 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']));test

    // 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

<?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

https://github.com/laiyuxiang/pdf_watermark/

相關文章
相關標籤/搜索