给定流在 FPDI 中不可搜索

Given stream is not seekable In FPDI

我正在使用 FPDI 库将多个 pdf 文件合并为一个,

关注此文档https://manuals.setasign.com/fpdi-manual/v2/the-fpdi-class/

我试过如下,

use \setasign\Fpdi\Fpdi;
use \setasign\Fpdi\PdfParser\StreamReader;
function merge()
{
    $file = fopen('https://path/to/s3/file','rb');
    $pdf = new Fpdi();
    $pdf->AddPage();
    $pdf->setSourceFile(new  StreamReader($file));
    $tplIdx = $pdf->importPage(1);
    $pdf->useTemplate($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();
}

当试图在 streamReader 中传递 url 时,我得到 给定的流不可搜索

如何将 s3 文件传递​​到流 reader 并合并它。

HTTP 流包装器不支持查找。

您必须将存储桶下载到临时文件或变量。一个简单的 file_get_contents() 就可以做到:

$fileContent = file_get_contents('https://path/to/s3/file','rb');
// ...
$pdf->setSourceFile(StreamReader::createByString($fileContent));

对于尝试从同一服务器加载文件的人(不适用于这种情况,但可以帮助其他人解决此错误):将http://example.com/path/file.pdf更改为像这样的本地路径 /home/user/public_html/path/file.pdf