From 8e536f239d3db82d448352ffe4c91ba61dd39a28 Mon Sep 17 00:00:00 2001 From: Babak Alizadeh Date: Sun, 9 Mar 2025 19:14:52 +0000 Subject: [PATCH] add debug for find problems --- .../src/Controller/GeneralController.php | 23 ++++++++---- hesabixCore/src/Service/pdfMGR.php | 36 ++++++++++--------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/hesabixCore/src/Controller/GeneralController.php b/hesabixCore/src/Controller/GeneralController.php index 943e458..390cf1f 100644 --- a/hesabixCore/src/Controller/GeneralController.php +++ b/hesabixCore/src/Controller/GeneralController.php @@ -23,14 +23,14 @@ class GeneralController extends AbstractController $version = '0.0.1'; $lastUpdateDate = '1399'; $lastUpdateDes = ''; - + $last = $entityManager->getRepository(ChangeReport::class)->findOneBy([], ['id' => 'DESC']); if ($last) { $version = $last->getVersion(); $lastUpdateDate = $jdate->jdate('Y/n/d', $last->getDateSubmit()); $lastUpdateDes = $last->getBody(); } - + return $this->json([ 'version' => $version, 'lastUpdateDate' => $lastUpdateDate, @@ -53,10 +53,10 @@ class GeneralController extends AbstractController } #[Route('/front/print/{id}', name: 'app_front_print')] - public function app_front_print(Provider $provider, EntityManagerInterface $entityManager, pdfMGR $pdfMGR, string $id): Response + public function app_front_print(Provider $provider, EntityManagerInterface $entityManager, PdfMGR $pdfMGR, string $id): Response { $print = $entityManager->getRepository(PrinterQueue::class)->findOneBy(['pid' => $id]); - + if (!$print) { return new JsonResponse(['error' => 'Print job not found'], Response::HTTP_NOT_FOUND); } @@ -65,12 +65,21 @@ class GeneralController extends AbstractController return new JsonResponse(['error' => 'Empty print content'], Response::HTTP_BAD_REQUEST); } + // تولید PDF و گرفتن محتوا به صورت رشته if ($print->isPosprint()) { - $pdfMGR->streamTwig2PDFInvoiceType($print); + $pdfContent = $pdfMGR->generateTwig2PDFInvoiceType($print); // متد جدید برای گرفتن محتوا } else { - $pdfMGR->streamTwig2PDF($print); + $pdfContent = $pdfMGR->generateTwig2PDF($print); // متد جدید برای گرفتن محتوا } - return new Response('Print job completed', Response::HTTP_OK); + // برگرداندن PDF به عنوان پاسخ + return new Response( + $pdfContent, + Response::HTTP_OK, + [ + 'Content-Type' => 'application/pdf', + 'Content-Disposition' => 'inline; filename="Hesabix PrintOut.pdf"', + ] + ); } } diff --git a/hesabixCore/src/Service/pdfMGR.php b/hesabixCore/src/Service/pdfMGR.php index 2e7aa2c..3d0b03e 100644 --- a/hesabixCore/src/Service/pdfMGR.php +++ b/hesabixCore/src/Service/pdfMGR.php @@ -17,25 +17,24 @@ class pdfMGR $this->twig = $twig; } - public function streamTwig2PDF(PrinterQueue $printQueue, $configs = []) + public function generateTwig2PDF(PrinterQueue $printQueue, $configs = []): string { - // Load Twig File $template = $this->twig->load('pdf/footer.html.twig'); $footer = $template->render([]); - + $size = $printQueue->getPaperSize() ?: 'A4-L'; $defaultConfig = (new ConfigVariables())->getDefaults(); $fontDirs = $defaultConfig['fontDir']; - + $defaultFontConfig = (new FontVariables())->getDefaults(); $fontData = $defaultFontConfig['fontdata']; - + $tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mpdf'; if (!is_dir($tempDir)) { mkdir($tempDir, 0777, true); } - + $mpdf = new Mpdf([ 'mode' => 'utf-8', 'format' => $size, @@ -52,31 +51,33 @@ class pdfMGR 'tempDir' => $tempDir, 'autoArabic' => true, ]); - + if ($printQueue->isFooter()) { $mpdf->SetHTMLFooter($footer); } - + $htmlContent = $printQueue->getView() ?: '

محتوای PDF در دسترس نیست.

'; $mpdf->WriteHTML($htmlContent); $mpdf->SetAutoPageBreak(true); $mpdf->SetTitle('حسابیکس'); - $mpdf->Output('Hesabix PrintOut.pdf', 'I'); + + // به جای Output مستقیم، محتوا رو برگردونید + return $mpdf->Output('', 'S'); // 'S' برای برگرداندن به صورت رشته } - - public function streamTwig2PDFInvoiceType(PrinterQueue $printQueue, $configs = []) + + public function generateTwig2PDFInvoiceType(PrinterQueue $printQueue, $configs = []): string { $defaultConfig = (new ConfigVariables())->getDefaults(); $fontDirs = $defaultConfig['fontDir']; - + $defaultFontConfig = (new FontVariables())->getDefaults(); $fontData = $defaultFontConfig['fontdata']; - + $tempDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'mpdf'; if (!is_dir($tempDir)) { mkdir($tempDir, 0777, true); } - + $mpdf = new Mpdf([ 'mode' => 'utf-8', 'format' => [80, 300], @@ -95,17 +96,18 @@ class pdfMGR 'autoArabic' => true, 'margin-collapse' => 'collapse|none' ]); - + $mpdf->AddPageByArray([ 'margin-left' => 0, 'margin-right' => 0, 'margin-top' => 0, 'margin-bottom' => 0, ]); - + $htmlContent = $printQueue->getView() ?: '

محتوای PDF در دسترس نیست.

'; $mpdf->WriteHTML($htmlContent); - $mpdf->Output(); + + return $mpdf->Output('', 'S'); // 'S' برای برگرداندن به صورت رشته } public function savePDF(PrinterQueue $printQueue, string $path)