php使用FPDI获取PDF内容、合并PDF文件、导入模板

使用FPDI获取PDF内容的方式

FPDI内部使用了StreamReader类,它使用低级函数,如fread()或ftell()来与流交互。
流读取器类提供了通过特定输入类型创建实例的静态方法。它的构造函数允许你传递任何可查找的开放流资源:

//资源流获取
$fh = fopen(ROOT_PATH.'public/test.pdf', 'rb');
$pdf->setSourceFile(new StreamReader($fh));
//等同于 $pdf->setSourceFile($fh);
fclose($fh);

//文件路径获取
$path = ROOT_PATH.'public/test.pdf';
$pdf->setSourceFile(StreamReader::createByFile($path));
//等同于 $pdf->setSourceFile($path);

//使用字符串方式获取
$pdfString = '%%PDF-1.4...';
//$pdfString = file_get_contents(ROOT_PATH.'public/test.pdf');
$pdf->setSourceFile(StreamReader::createByString($pdfString));

例子

<?php
use \setasign\Fpdi\Fpdi;

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

$pdf = new Fpdi();
$pdf->AddPage();
//设置要导入的文件
$pdf->setSourceFile("Fantastic-Speaker.pdf");
//导入第一页
$tplIdx = $pdf->importPage(1);
//使用模板
$pdf->useImportedPage($tplIdx, 10, 10, 100);

$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');

$pdf->Output();

FPDI官网地址 https://manuals.setasign.com/fpdi-manual/v2/

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值