title: Generate a PDF and return a response
body: <p>Today I have faced a problem with generating PDF using KnpSnappyBundle on symfony.</p>
  
  <p>I'm running a route using AJAX, the code works fine, but it does not generate the PDF,</p>
  
  <p>What I want to accomplish here is to generate a PDF in new tab or window
  and return a successful message to the client.</p>
  
  <p>Here is my code:</p>
  
  <pre><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&gt;0 &amp;&amp; $month&lt;13 || preg_match("/^\d{4}$/",$year)||$region!=''){
  
          if($year&gt;=2018 &amp;&amp; $year &lt;=2050){
              $entityManager = $this-&gt;getDoctrine()-&gt;getManager();
  
                  $bills = $consumptionRepository-&gt;findAllWaterMetersForBilling($month,$year,$region);
  
                  $isBillsExisted = $billRepository-&gt;findAllBillsByDate($month,$year,$region);
  
                  if(empty($bills)){
                      array_push($error,['error'=&gt;'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=&gt;$b){
                      $wmNumber =  $b-&gt;getWaterMeter()-&gt;getWmNumber();
                      $fullName = $b-&gt;getWaterMeter()-&gt;getClient()-&gt;getFullName();
                      $cin = $b-&gt;getWaterMeter()-&gt;getClient()-&gt;getCin();
                      $address= $b-&gt;getWaterMeter()-&gt;getAddress();
                      $wmAddress = $b-&gt;getWaterMeter()-&gt;getAddress()-&gt;getStreetAddress();
                      $city = $b-&gt;getWaterMeter()-&gt;getAddress()-&gt;getCity();
                      $zipCode = $b-&gt;getWaterMeter()-&gt;getAddress()-&gt;getZipCode();
                      $billCode = $b-&gt;getId();
                      $consumptionDate = $b-&gt;getDate();
                      $oldConsumption=$b-&gt;getPreviousRecord();
                      $newConsumption=$b-&gt;getCurrentRecord();
                      $printDate =new \DateTime('now');
                      $consumption=$b-&gt;getConsumption();
                      $cost = $b-&gt;getCost()+10;
                      $wmReferenceNumber = $b-&gt;getWaterMeter()-&gt;getWmReferenceNumber();
                      $client= $b-&gt;getWaterMeter()-&gt;getClient();
                      // tranche calculation
                      //==================================================
                      $tranche1 = $consumption &gt;= 5 ? 5 : $consumption; //4
                      $tranche2 = $consumption - $tranche1 &gt;= 5 ? 5: 0; //0
                      $tranche3 = $consumption - $tranche1 - $tranche2;
                      //==================================================
                      $bill = new Bill();
                      $bill-&gt;setCode($wmNumber);
                      $bill-&gt;setFullName($fullName);
                      $bill-&gt;setCin($cin);
                      $bill-&gt;setPrintDate($printDate);
                      $bill-&gt;setCost($cost);
                      $bill-&gt;setConsumption($consumption);
                      $bill-&gt;setConsumptionDate($consumptionDate);
                      $bill-&gt;setPreviousRecord($oldConsumption);
                      $bill-&gt;setNewRecord($newConsumption);
                      $bill-&gt;setWaterMeter($b-&gt;getWaterMeter());
                      $bill-&gt;setRegion($city);
                      $bill-&gt;setClient($client);
                      $entityManager-&gt;persist($bill);
                      array_push($arr,['wmAddress'=&gt;$wmAddress,'city'=&gt;$city,'zipCode'=&gt;$zipCode,'tranche1'=&gt;$tranche1,'wmNumber'=&gt;$wmNumber,'fullName'=&gt;$fullName,'cin'=&gt;$cin,'address'=&gt;strval($address),'billCode'=&gt;$billCode,'consumptionDate'=&gt;$consumptionDate,'oldConsumption'=&gt;$oldConsumption,'newConsumption'=&gt;$newConsumption,'printDate'=&gt;$printDate,'consumption'=&gt;$consumption,'cost'=&gt;$cost,'wmReferenceNumber'=&gt;$wmReferenceNumber]);
  
                      if(count($arr)==6||$billCode==$billsEnd-&gt;getId()){
                          array_push($newBills,$arr);
                          $arr = array();
                      }
  
                  }
  
                  $date = new \DateTime('now');
                  $html= $this-&gt;renderView('bill/bill.html.twig', array(
                      'arrayBills'=&gt;$newBills,
                  ));
  
  
                  if($isBillsExisted){
                      return new PdfResponse(
                          $snappy-&gt;getOutputFromHtml($html), "bill_".$date-&gt;format('Y-m-d').".pdf"
                      );
                  }
                  $entityManager-&gt;flush(); //Persist objects that did not make up an entire batch
                  $entityManager-&gt;clear();
  
                  return new PdfResponse(
                      $snappy-&gt;getOutputFromHtml($html), "bill_".$date-&gt;format('Y-m-d').".pdf"
                  );
  
              }
          }
      array_push($success,['success'=&gt;'bills were successfully released']);
      return new JsonResponse($success);
  }
  </code></pre>
  
  <p>And my AJAX call is:</p>
  
  <pre><code> generatePDF = () =&gt;{ 
          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)
          });
      };
  </code></pre>
  
  <p>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.</p>