Getting error while exporting pdf from custom view.

Hello Team,

I would like to share some issues we're currently facing. I would appreciate it if you could check this on high priority as one of our existing clients is facing this.

we have a functionality to export a PDF but we have encountered an issue.
In this pdf we also use the images created at the time of export.
And the function used for creating the image sugar functions which is

$UploadStream = new UploadStream();
$ifp = $UploadStream->stream_open('123456789' . $id . $final_ext, "wb");
$UploadStream->stream_write(base64_decode($image_data[1]));
$UploadStream->stream_close();

These images will be created in upload folder during PDF generation. Sometimes, the PDFs are created and exported successfully, but occasionally, we receive a 500/TCPDF image not found error.
We discovered that the images created in the upload folder during PDF generation are sometimes corrupted, which causes the PDF creation to fail.

Here attaching the video link and screenshot as well.
www.awesomescreenshot.com/.../28605318

In this video, you can see that the first time the PDF downloads perfectly, but on the second attempt, we get an error. In the upload folder, one file is created corrupted, which causes the 500 error.

I am also sharing the error.log file, which contains the error log when the PDF export fails.

PHP Warning:  Undefined array key "trend" in /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/custom/include/utilsfunction.php on line 4050, referer: http://localhost/SugarEnt1400Jaun/SugarEnt1400Jaun/
[Thu Jun 13 16:38:22.013303 2024] [php:warn] [pid 1399] [client ::1:41228] PHP Warning:  imagecreatefrompng(): gd-png: fatal libpng error: IDAT: CRC error\n in /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php on line 5187, referer: http://localhost/SugarEnt1400Jaun/SugarEnt1400Jaun/
[Thu Jun 13 16:38:22.013334 2024] [php:warn] [pid 1399] [client ::1:41228] PHP Warning:  imagecreatefrompng(): gd-png error: setjmp returns error condition 3\n in /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php on line 5187, referer: http://localhost/SugarEnt1400Jaun/SugarEnt1400Jaun/
[Thu Jun 13 16:38:22.013365 2024] [php:warn] [pid 1399] [client ::1:41228] PHP Warning:  imagecreatefrompng(): "upload/3f3262ec-2975-11ef-a6a7-106fd94da741.png" is not a valid PNG file in /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php on line 5187, referer: http://localhost/SugarEnt1400Jaun/SugarEnt1400Jaun/
[Thu Jun 13 16:38:22.013614 2024] [php:error] [pid 1399] [client ::1:41228] PHP Fatal error:  Uncaught TypeError: imagecolorat(): Argument #1 ($image) must be of type GdImage, bool given in /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php:5196\nStack trace:\n#0 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php(5196): imagecolorat()\n#1 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php(4864): TCPDF->ImagePngAlpha()\n#2 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php(12980): TCPDF->Image()\n#3 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php(12630): TCPDF->openHTMLTagHandler()\n#4 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/custom/include/utilsfunction.php(5071): TCPDF->writeHTML()\n#5 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/custom/include/utilsfunction.php(5041): generateExportReportInPdf()\n#6 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/custom/biz/classes/exportReportsData.php(82): exportQuestionNormalReportDataAsPDF()\n#7 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/include/MVC/Controller/SugarController.php(890): require_once('...')\n#8 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/include/MVC/Controller/SugarController.php(415): SugarController->handleEntryPoint()\n#9 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/include/MVC/Controller/SugarController.php(328): SugarController->process()\n#10 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/include/MVC/SugarApplication.php(207): SugarController->execute()\n#11 /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/index.php(29): SugarApplication->execute()\n#12 {main}\n  thrown in /var/www/html/SugarEnt1400Jaun/SugarEnt1400Jaun/vendor/tcpdf/tcpdf.php on line 5196, referer: http://localhost/SugarEnt1400Jaun/SugarEnt1400Jaun/