GrofGraf / laravel-pdf-merger

Package for Laravel that merges multiple PDFs into one.
MIT License
29 stars 44 forks source link

FPDF error: The document is closed #4

Open Robertbaelde opened 5 years ago

Robertbaelde commented 5 years ago

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?

GrofGraf commented 5 years ago

@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())?

Robertbaelde commented 5 years ago

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());
GrofGraf commented 5 years ago

@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.

matrad commented 5 years ago

The issue ocurrs when merging multiple files in a foreach loop

easydeveloppement commented 5 years ago

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 ?

coupej commented 4 years ago

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

easydeveloppement commented 4 years ago

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 ?