生成 PDF 并返回响应

今天,我遇到了一个问题,即在符号上使用KnpSnappyBundle生成PDF。


我正在使用AJAX运行路由,代码工作正常,但它不会生成PDF,


我想在这里完成的是在新的选项卡或窗口中生成PDF,并向客户端返回成功的消息。


这是我的代码:


 /**

 * @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();



动漫人物
浏览 162回答 3
3回答

慕沐林林

可能是以后的事了,但你可以像这样尝试:&nbsp; &nbsp; $file = $folder . $fileName ;&nbsp; &nbsp; if (is_file($file)) {&nbsp; &nbsp; &nbsp; &nbsp; unlink($file);&nbsp; &nbsp; }然后,生成并保存具有相同名称及之后的文件。不要使用,但return new PdfResponse...$pdf->generateFromHtml($html, $folder . $file);,然后返回响应&nbsp; &nbsp; $response = new JsonResponse();&nbsp; &nbsp; $response->setStatusCode(Response::HTTP_NOT_FOUND); // 404&nbsp; &nbsp; if (is_file($file)) {&nbsp; &nbsp; &nbsp; &nbsp; $response->setStatusCode(Response::HTTP_OK); // 200&nbsp; &nbsp; }

九州编程

尝试像这样修改阿贾克斯:$.ajax({&nbsp; url: `http://symfony.localhost/bill/api/release_bills/${monthValue.value}/${yearValue.value}/${regionValue.value}`,&nbsp; success: function(data) {&nbsp; &nbsp; var blob=new Blob([data]);&nbsp; &nbsp; var link=document.createElement('a');&nbsp; &nbsp; link.href=window.URL.createObjectURL(blob);&nbsp; &nbsp; link.download="<CHOOSE_FILENAME_WITH_EXTENSION>";&nbsp; &nbsp; link.click();&nbsp; }});

炎炎设计

在朋友的帮助下,我设法生成pdf,但我仍然不知道如何在生成Pdf时返回错误或成功消息。这是我所做的:generatePDF = () =>{&nbsp; &nbsp; const {yearValue, monthValue, regionValue}= this.state;&nbsp; &nbsp; var strWindowFeatures = "location=yes,height=570,width=520,scrollbars=yes,status=yes";&nbsp; &nbsp; var URL = `http://symfony.localhost/bill/api/release_bills/${monthValue.value}/${yearValue.value}/${regionValue.value}`;&nbsp; &nbsp; window.open(URL, "_blank", strWindowFeatures);};函数生成PDF在单击按钮上被调用
打开App,查看更多内容
随时随地看视频慕课网APP