4

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.

Manjunath C
  • 65
  • 1
  • 7
  • Not exactly sure why you would need the Stream Reader. Have you tried the [default merger](https://manuals.setasign.com/setapdf-merger-manual/) and tried [these methods?](https://manuals.setasign.com/setapdf-merger-manual/add-files-or-documents/#index-2) – Jamie_D Dec 12 '18 at 06:42
  • Hi @Jamie_D, I have used Stream Reader to read files from s3 bucket, **setSourceFile** takes only local path. – Manjunath C Dec 12 '18 at 06:45
  • Assuming that your S3 pdf file is public, try `$file =Document SetaPDF_Core_Document::loadByString( file_get_contents('https://path/to/s3/file') );` See [example here](https://manuals.setasign.com/setapdf-merger-manual/add-files-or-documents/#index-2) – Jamie_D Dec 12 '18 at 06:48

2 Answers2

10

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));
Jan Slabon
  • 4,736
  • 2
  • 14
  • 29
  • Hi, this works perfectly on my local machine but when hosting through AWS Lambda using Laravel Vapor, I get a 503 error. Any idea what the cause could be? Been battling with this for weeks now... – Adam Sep 08 '21 at 08:15
  • 503 means "Service Unavailable". Maybe [this](https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/http-503-service-unavailable.html) will help you. – Jan Slabon Sep 08 '21 at 09:34
  • It workerd for me withou the 'rb' parameter. – achasinh Feb 15 '23 at 08:39
  • @achasinh sure, I just removed it. – Jan Slabon Feb 16 '23 at 06:53
3

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