如何在 symfony 4 的新选项卡中打开生成的 pdf

在我的Symfony 4应用程序上,我有一个用于从 html 生成 pdf 的按钮,knp snappy bundle但生成的 pdf 显示在同一页面中,所以我正在寻找一种在新选项卡上打开生成的 pdf 的方法,有没有办法完成那 ?

回首忆惘然
浏览 109回答 2
2回答

噜噜哒

您可以发布代码吗?但是,您应该在打开 pdf 的位置添加target="_blank" ,如下所示:<a&nbsp;target="_blank"&nbsp;href="http://your_url.html">Link&nbsp;to&nbsp;the&nbsp;route&nbsp;that&nbsp;generates&nbsp;the&nbsp;pdf</a>

ibeautiful

这是我的收据操作,我创建了新收据,然后打印它:/**&nbsp;* @Route("/receipt/{id}", name="receipt_index", methods={"GET","POST"})&nbsp;* @param Bill $bill&nbsp;* @return Response&nbsp;* @throws Exception&nbsp;*/public function newReceipt(Bill $bill): Response{&nbsp; &nbsp; $receipt = new Receipt();&nbsp; &nbsp; $date = new \DateTime('now');&nbsp; &nbsp; $receipt->setBill($bill);&nbsp; &nbsp; $receipt->setBillCost($bill->getCost());&nbsp; &nbsp; $receipt->setBillDate($bill->getPrintDate());&nbsp; &nbsp; $receipt->setBillNumber($bill->getId());&nbsp; &nbsp; $receipt->setClientName($bill->getClient()->getFullName());&nbsp; &nbsp; $receipt->setReceiptDate($date);&nbsp; &nbsp; $receipt->getBill()->setStatus(true);&nbsp; &nbsp; $binary = $this->container->getParameter('knp_snappy.pdf.binary');&nbsp; &nbsp; $snappy = new Snappy($binary);&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; $newDate= $date->format('Y-m-d');&nbsp; &nbsp; &nbsp; &nbsp; $entityManager = $this->getDoctrine()->getManager();&nbsp; &nbsp; &nbsp; &nbsp; $entityManager->persist($receipt);&nbsp; &nbsp; &nbsp; &nbsp; $entityManager->flush();&nbsp; &nbsp; &nbsp; &nbsp; $html= $this->renderView('bill/receipt.html.twig', array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'receipt'=>$receipt,&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $pdf=$snappy->getOutputFromHtml($html,array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'orientation' => 'portrait',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'enable-javascript' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'javascript-delay' => 1000,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'no-stop-slow-scripts' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'no-background' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'encoding' => 'utf-8',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'lowquality' => false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page-width' => '8cm',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'page-height' => '12.40cm',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'margin-left'=>0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'margin-right'=>0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'margin-top'=>0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'margin-bottom'=>0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'images' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'cookie' => array(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'dpi' => 300,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'enable-external-links' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'enable-internal-links' => true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; return new Response($pdf,200,array(&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'application/pdf',&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Disposition'&nbsp; &nbsp;=> 'inline; filename="recu-'.$newDate.'.pdf"'&nbsp; &nbsp; ));&nbsp; &nbsp; } catch (Exception $e){&nbsp; &nbsp; &nbsp; &nbsp; dump($e);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP