Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given stream is not seekable In FPDI

I am using FPDI library to merge multiple pdf files into one,

followed this documentaion https://manuals.setasign.com/fpdi-manual/v2/the-fpdi-class/

I have tried like below,

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();
}

When tried to pass url in streamReader I am getting Given stream is not seekable.

How can I pass s3 file to stream reader and merge it.

like image 727
Manjunath C Avatar asked Mar 04 '23 22:03

Manjunath C


2 Answers

An HTTP stream wrapper does not support seeking.

You have to download the bucket to a temporary file or variable. A simple file_get_contents() should do it:

$fileContent = file_get_contents('https://path/to/s3/file');
// ...
$pdf->setSourceFile(StreamReader::createByString($fileContent));
like image 96
Jan Slabon Avatar answered Mar 13 '23 00:03

Jan Slabon


For those who try to load the file from the same server (not apply to this case but could help others solve this error): change http://example.com/path/file.pdf to a local path like this /home/user/public_html/path/file.pdf

like image 44
Andrei Manolache Avatar answered Mar 13 '23 02:03

Andrei Manolache