Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined array key "" #3384

Open
nemanja-ivanis opened this issue Feb 2, 2024 · 9 comments
Open

Undefined array key "" #3384

nemanja-ivanis opened this issue Feb 2, 2024 · 9 comments

Comments

@nemanja-ivanis
Copy link

When i try to output my pdf i get "Undefined array key ''" on line 4752 in dompdf\dompdf\lib\Cpdf.php. That particular line of code is:

$font = $this->fonts[$this->currentFont];

Both $this->fonts and $this->currentFont are empty. Why is this happening and how can i fix it?

@bsweeney
Copy link
Member

bsweeney commented Feb 2, 2024

Those should never be empty, what does your code look like on the PHP side of things?

@nemanja-ivanis
Copy link
Author

nemanja-ivanis commented Feb 2, 2024

$pdf = \PDF::loadView('layouts.pdf', compact('otherContact', 'currentDate', 'companyData', 'clinic', 'employee', 'startDate', 'endDate', 'qualification'))->setPaper('a4', 'portrait');
Storage::disk('local')->put('clinics/' . $clinic->id . '/contracts/' . $pdfName, $pdf->output());

I have no idea why are those empty...

A note - i am using https://github.com/barryvdh/laravel-dompdf but the error comes from dompdf/dompdf library.

@bsweeney
Copy link
Member

bsweeney commented Feb 2, 2024

I'm not a laravel user, so you might ask on that project. Perhaps you need to update the fontDir configuration?

@nemanja-ivanis
Copy link
Author

I tried, but with no luck.

@bsweeney
Copy link
Member

bsweeney commented Feb 4, 2024

What is layouts.pdf? Is that a PDF document or a Laravel blade template that just happens to have .pdf in its name?

@legibe-com
Copy link

any updates on this, by any chance?

just updated from dompdf 2.0.3 to dompdf 3, my script worked perfectly before the update and i now have the same problem, the line number has changed a bit compared to this post's first message but that's all:

Warning: Undefined array key "" in [...] composer/vendor/dompdf/dompdf/lib/Cpdf.php on line 4753

these are the options i have:

$options = new Options;
$options->set('isPhpEnabled', 'true');
$options->setChroot(__DIR__);
$options->setIsRemoteEnabled(true);
$dompdf = new Dompdf($options);

i tried to add:

$options->set('defaultFont', 'Helvetica');

as well as this:

$dompdf->getFontMetrics()->getFont('Helvetica', 'normal');

and then:

$fontDir = __DIR__ . '/path/to/fonts';
$options->set('fontDir', $fontDir);

but it doesn't change anything. and yes, i adapted the /path/to/fonts in the code.

any ideas?

@legibe-com
Copy link

ok, i replaced all my options with this code found in the documentation:

$dompdf_tmp_dir = sys_get_temp_dir();
$dompdf = new Dompdf([
	'logOutputFile' => '',
	'isRemoteEnabled' => true,
	'fontDir' => $dompdf_tmp_dir,
	'fontCache' => $dompdf_tmp_dir,
	'defaultFont' => 'Helvetica',
	'tempDir' => $dompdf_tmp_dir,
	'chroot' => $dompdf_tmp_dir,
]);
$dompdf->setPaper("A4", "portrait");

still unsure why, but now it works!

@bsweeney
Copy link
Member

@legibe-com curious ... if you remove the "defaultFont" option does the error return?

@legibe-com
Copy link

legibe-com commented Jun 10, 2024

it doesn't! i can actually remove the $dompdf_tmp_dir line and just keep this:

$dompdf = new Dompdf([
	'isRemoteEnabled' => true
]);

since my html template uses external css and font files, and it works. if i remove this option it works too, but without the right css formatting.

i suppose that keeping the old code to set default variables etc was creating a conflict, somehow...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants