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

FPDF error: The document is closed

Open duikb00t opened this issue 5 years ago • 3 comments

Hi,

I have the issue when I try to merge multiple PDF's that it gives me the error:

[2019-04-08 12:21:50] dev.ERROR: FPDF error: The document is closed {"exception":"[object] (Exception(code: 0): FPDF error: The document is closed at /project/vendor/setasign/fpdf/fpdf.php:271)
[stacktrace]

#0 /project/vendor/setasign/fpdf/fpdf.php(295): FPDF->Error('The document is...')
#1 /project/vendor/setasign/fpdi/src/FpdfTplTrait.php(329): FPDF->AddPage('P', Array, 0)
#2/project/vendor/grofgraf/laravel-pdf-merger/src/PDFMerger.php(177): setasign\\Fpdi\\FpdfTpl->AddPage('P', Array)
#3 /project/app/Jobs/ProcessCoupons.php(49): GrofGraf\\LaravelPDFMerger\\PDFMerger->merge()
#4 [internal function]: App\\Jobs\\ProcessCoupons->handle(
$merger = \PDFMerger::init();

$registration->coupons->each(function($coupon) use ($merger) {
$pdf = Storage::disk('coupons')->path($coupon->file_path);
    $merger->addPDFString($pdf);
});

$merger->merge();
$merger->save(sprintf('%s_%s_%s.%s', 'coupon_merged', time(), mt_rand(1000, 9999), 'pdf'));

My Setup:

"grofgraf/laravel-pdf-merger": "^1.0",
"laravel/framework": "5.5.*",
"php": ">=7.1.0",

What causes the issue that it's returning that error? It creates the tmp folder with temporary PDFs but it's not merging.

duikb00t avatar Apr 08 '19 10:04 duikb00t

same issue here, identical setup but php7.2

matrad avatar Apr 16 '19 06:04 matrad

I have a similar problem. As long as I run it in a webbrowser it seems to work fine, but when using a background worker the problem occurs. Probably because the internatl Fpdi instance isn't refreshed correctly by the INIT function...

coupej avatar Nov 21 '19 14:11 coupej

Explicity resetting all attributes seem to fix the issue, but wouldn't recommend ;-).

    $this->fpdi =null; 
    $this->fpdi = new Fpdi();
    $this->tmpFiles = null; 
    $this->tmpFiles = collect([]);
    $this->files = null; 
    $this->files = collect([]);

coupej avatar Nov 21 '19 15:11 coupej