Generate a PDF and return a response

Today I have faced a problem with generating PDF using KnpSnappyBundle on symfony.

I’m running a route using AJAX, the code works fine, but it does not generate the PDF,

What I want to accomplish here is to generate a PDF in new tab or window
and return a successful message to the client.

Here is my code:

 /**
 * @Route("/api/release_bills/{month}/{year}/{region}", name="api_print_bills", methods={"GET","POST"})
 * @param BillRepository $billRepository
 * @param ConsumptionRepository $consumptionRepository
 * @param Request $request
 * @param Snappy $snappy
 * @param $month
 * @param $year
 * @param $region
 * @return Response
 * @throws Exception
 */
public function releaseBills(BillRepository $billRepository,ConsumptionRepository $consumptionRepository, Request $request, Snappy $snappy,  $month,$year,$region ): Response
{
    $error = array();
    $success = array();
    if( $month>0 && $month<13 || preg_match("/^d{4}$/",$year)||$region!=''){

        if($year>=2018 && $year <=2050){
            $entityManager = $this->getDoctrine()->getManager();

                $bills = $consumptionRepository->findAllWaterMetersForBilling($month,$year,$region);

                $isBillsExisted = $billRepository->findAllBillsByDate($month,$year,$region);

                if(empty($bills)){
                    array_push($error,['error'=>'there is not bills to be release for this criteria : [ city: '.$region.', year :'.$year.', and Month : '.$month.'!]']);
                    return new JsonResponse($error);
                }

                $arr = array();
                $newBills = array();
                $billsEnd=end($bills);

                foreach ($bills as $key=>$b){
                    $wmNumber =  $b->getWaterMeter()->getWmNumber();
                    $fullName = $b->getWaterMeter()->getClient()->getFullName();
                    $cin = $b->getWaterMeter()->getClient()->getCin();
                    $address= $b->getWaterMeter()->getAddress();
                    $wmAddress = $b->getWaterMeter()->getAddress()->getStreetAddress();
                    $city = $b->getWaterMeter()->getAddress()->getCity();
                    $zipCode = $b->getWaterMeter()->getAddress()->getZipCode();
                    $billCode = $b->getId();
                    $consumptionDate = $b->getDate();
                    $oldConsumption=$b->getPreviousRecord();
                    $newConsumption=$b->getCurrentRecord();
                    $printDate =new DateTime('now');
                    $consumption=$b->getConsumption();
                    $cost = $b->getCost()+10;
                    $wmReferenceNumber = $b->getWaterMeter()->getWmReferenceNumber();
                    $client= $b->getWaterMeter()->getClient();
                    // tranche calculation
                    //==================================================
                    $tranche1 = $consumption >= 5 ? 5 : $consumption; //4
                    $tranche2 = $consumption - $tranche1 >= 5 ? 5: 0; //0
                    $tranche3 = $consumption - $tranche1 - $tranche2;
                    //==================================================
                    $bill = new Bill();
                    $bill->setCode($wmNumber);
                    $bill->setFullName($fullName);
                    $bill->setCin($cin);
                    $bill->setPrintDate($printDate);
                    $bill->setCost($cost);
                    $bill->setConsumption($consumption);
                    $bill->setConsumptionDate($consumptionDate);
                    $bill->setPreviousRecord($oldConsumption);
                    $bill->setNewRecord($newConsumption);
                    $bill->setWaterMeter($b->getWaterMeter());
                    $bill->setRegion($city);
                    $bill->setClient($client);
                    $entityManager->persist($bill);
                    array_push($arr,['wmAddress'=>$wmAddress,'city'=>$city,'zipCode'=>$zipCode,'tranche1'=>$tranche1,'wmNumber'=>$wmNumber,'fullName'=>$fullName,'cin'=>$cin,'address'=>strval($address),'billCode'=>$billCode,'consumptionDate'=>$consumptionDate,'oldConsumption'=>$oldConsumption,'newConsumption'=>$newConsumption,'printDate'=>$printDate,'consumption'=>$consumption,'cost'=>$cost,'wmReferenceNumber'=>$wmReferenceNumber]);

                    if(count($arr)==6||$billCode==$billsEnd->getId()){
                        array_push($newBills,$arr);
                        $arr = array();
                    }

                }

                $date = new DateTime('now');
                $html= $this->renderView('bill/bill.html.twig', array(
                    'arrayBills'=>$newBills,
                ));


                if($isBillsExisted){
                    return new PdfResponse(
                        $snappy->getOutputFromHtml($html), "bill_".$date->format('Y-m-d').".pdf"
                    );
                }
                $entityManager->flush(); //Persist objects that did not make up an entire batch
                $entityManager->clear();

                return new PdfResponse(
                    $snappy->getOutputFromHtml($html), "bill_".$date->format('Y-m-d').".pdf"
                );

            }
        }
    array_push($success,['success'=>'bills were successfully released']);
    return new JsonResponse($success);
}

And my AJAX call is:

 generatePDF = () =>{ 
        const {yearValue, monthValue, regionValue}= this.state;
        $.ajax({
            url: `http://symfony.localhost/bill/api/release_bills/${monthValue.value}/${yearValue.value}/${regionValue.value}`,
            success: function (data) {
                console.log('DATA',data)
            }.bind(this)
        });
    };

I believe that I can’t return more than one response, anyway I have done console log to the response of the ajax call displays the HTML content of the page I want to generate a PDF from and no PDF is being generated.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *