laravel-pdf-merger icon indicating copy to clipboard operation
laravel-pdf-merger copied to clipboard

FPDF error: The document is closed

Open Robertbaelde opened this issue 5 years ago • 7 comments

Hi,

I got the following error in my error log, when merging pdf's.

The code that gives the error, and interacts with the package:

        $merger = \PDFMerger::init();
        $this->tickets->each(function(Ticket $ticket) use ($merger) {
            $merger->addPDFString($ticket->getRawPdf(), 'all', 'P');
        });
        $merger->merge();
        return $merger->string();

Am i doing something wrong here? Or is there an issue in the package/underlaying library?

Robertbaelde avatar Apr 02 '19 15:04 Robertbaelde

@Robertbaelde Hi,

at first glance everything looks fine.

Maybe you could provide some more code and we can try to figure out what the problem is.

What package are you using for generating and displaying PDFs?

What is the output if you dump dd($ticket->getRawPdf())?

GrofGraf avatar Apr 03 '19 07:04 GrofGraf

I'm using dompdf for the generation of the pdf's. The getRawPdf just returns the contents of the pdf

return Storage::disk( 'tickets-'.config('app.env'))->get($this->getPath());

Robertbaelde avatar Apr 03 '19 20:04 Robertbaelde

@Robertbaelde Looking at the error code it seems to me that you are calling merge() method somewhere, after you called the output method string(). The output method terminates the PDF, so it should be called only once in the end.

GrofGraf avatar Apr 04 '19 08:04 GrofGraf

The issue ocurrs when merging multiple files in a foreach loop

matrad avatar Apr 16 '19 06:04 matrad

I have the same issue, i am in a foreach loop also. The code outside the loop works fine, but inside the loop, i get the same error.

It works fine for the 1st element in the loop, but fail from the next one.

It looks like we cannot use PDFMerger several times on the same page ?

easydeveloppement avatar Sep 19 '19 05:09 easydeveloppement

Did anyone find the solution/pointer on this. Especially in a backgound worker context this issue is killing me.

coupej avatar Nov 21 '19 14:11 coupej

Hello, it seems that we can use "$merger = app('\GrofGraf\LaravelPDFMerger\PDFMerger');" instead of the facade, and it seems to work properly.

the problem happen only when we use several times pdfmerger on the same page, because it is used as a singleton.

May be there is a function to call after merge() to initialize it again ?

easydeveloppement avatar Apr 08 '20 12:04 easydeveloppement