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 Error in Font Insertion #116

Open
Diar44 opened this issue Oct 12, 2024 · 0 comments
Open

Undefined Array Key Error in Font Insertion #116

Diar44 opened this issue Oct 12, 2024 · 0 comments

Comments

@Diar44
Copy link

Diar44 commented Oct 12, 2024

Hello,

I encountered an issue when trying to insert a font into my PDF using TCPDF. The following code results in an error:

$pdf->font->insert($pdf->pon, 'helvetica', '', 10, 2, 2, $this->getParameter('k_path_fonts') . "/helvetica.json");
The error message is as follows:

Uncaught PHP Exception ErrorException: "Warning: Undefined array key 1" at Stack.php line 583
Upon reviewing the Stack.php file, I found that the issue is related to the code in lines 579–588, which deals with assigning the font bounding box values:

$this->metric[$mkey]['fbbox'] = [
// left
((float) $tbox[0] * $wratio),
// bottom
((float) $tbox[1] * $cratio),
// right
((float) $tbox[2] * $wratio),
// top
((float) $tbox[3] * $cratio),
];
The problem is that in my case, the $tbox array only contains tbox[0], and tbox[1] is missing, causing the undefined key error.

As a workaround, I modified the code to use the null coalescing operator (??) to provide default values in case any of the indices are missing:

$this->metric[$mkey]['fbbox'] = [
((float) ($tbox[0] ?? 0) * $wratio),
((float) ($tbox[1] ?? 0) * $cratio),
((float) ($tbox[2] ?? 0) * $wratio),
((float) ($tbox[3] ?? 0) * $cratio),
];
This approach resolved the issue for me. However, I am unsure if this is the best solution or if a more appropriate fix should be applied.

Could you please advise on whether this is a suitable fix, or if there might be a deeper issue with the $tbox array that needs to be addressed?

Best regards,
Diar

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

1 participant