php fpdf fpdi 操作pdf类库

张映 发表于 2018-07-07

分类目录: php

标签:, ,

以前也用过一些操作pdf的类库。fpdf,fpdi比较强大的类库。

1,下载fpdf,fpdi

http://www.fpdf.org

https://www.setasign.com/products/fpdi/downloads/#p-313

fpdi可以用composer下载

2,合并多个pdf

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;

require_once('fpdf/fpdf.php');
require_once('fpdi/src/autoload.php');

// define some files to concatenate
$files = array(
    'test.pdf',
    't11.pdf'
);  

// initiate FPDI
$pdf = new Fpdi();  

// iterate through the files
foreach ($files AS $file) {
    // get the page count
    $pageCount = $pdf->setSourceFile($file);
    // 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);
//  		print_r($size);break;
        // create a page (landscape or portrait depending on the imported page size)
        if ($size['width'] > $size['height']) {
            $pdf->AddPage('L', array($size['width'], $size['height']));
        } else {
            $pdf->AddPage('P', array($size['width'], $size['height']));
        }  

        // use the imported page
        $pdf->useTemplate($templateId);  

        $pdf->SetFont('Helvetica');
        $pdf->SetXY(5, 5);
//        $pdf->Write(8, 'A simple concatenation demo with FPDI');
    }
}  

// Output the new PDF
//$pdf->Output();     //直接输出

$pdf->output("merge.pdf", "F");   //下载到本地

3,往PDF文件尾部追加一张图片

use setasign\Fpdi\Fpdi;
use setasign\Fpdi\PdfReader;

require_once('fpdf/fpdf.php');
require_once('fpdi/src/autoload.php');

// initiate FPDI
$pdf = new Fpdi();  

// get the page count
$pageCount = $pdf->setSourceFile('t11.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['width'] > $size['height']) {
        $pdf->AddPage('L', array($size['width'], $size['height']));
    } else {
        $pdf->AddPage('P', array($size['width'], $size['height']));
    }  

    // use the imported page
    $pdf->useTemplate($templateId);  

    $pdf->SetFont('Helvetica');
    $pdf->SetXY(5, 5);
}  

$imagenurl = "testtest.png"; // in my case

//$pdf->AliasNbPages();
$pdf->AddPage("L","A4");
$pdf->SetFont('Times','',12);

// Add image to document's content (Change 100 to the image's width)
$pdf->Cell( 38, 38, $pdf->Image($imagenurl, $pdf->GetX(), $pdf->GetY(), 280), 0, 0, 'L', false );
$pdf->Output();

 



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/php/1938.html

留下评论

留下评论
  • (必需)
  • (必需) (will not be published)
  • (必需)   5X1=?