如何在 symfony 4 的新标签页中打开生成的 pdf
how to open the generated pdf in new tab on symfony 4
在我的 Symfony 4
应用程序上,我有一个用于使用 knp snappy bundle
从 html 生成 pdf 的按钮,但生成的 pdf 显示在同一页面中,所以我寻找一种在新选项卡上打开生成的 pdf 的方法,有没有办法做到这一点?
提前谢谢你。
你能post代码吗?
但是您应该添加 target="_blank",其中打开 pdf 的位置如下:
<a target="_blank" href="http://your_url.html">Link to the route that generates the pdf</a>
这是我的收据操作,我创建了新的收据然后打印了它:
/**
* @Route("/receipt/{id}", name="receipt_index", methods={"GET","POST"})
* @param Bill $bill
* @return Response
* @throws Exception
*/
public function newReceipt(Bill $bill): Response
{
$receipt = new Receipt();
$date = new \DateTime('now');
$receipt->setBill($bill);
$receipt->setBillCost($bill->getCost());
$receipt->setBillDate($bill->getPrintDate());
$receipt->setBillNumber($bill->getId());
$receipt->setClientName($bill->getClient()->getFullName());
$receipt->setReceiptDate($date);
$receipt->getBill()->setStatus(true);
$binary = $this->container->getParameter('knp_snappy.pdf.binary');
$snappy = new Snappy($binary);
try {
$newDate= $date->format('Y-m-d');
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($receipt);
$entityManager->flush();
$html= $this->renderView('bill/receipt.html.twig', array(
'receipt'=>$receipt,
));
$pdf=$snappy->getOutputFromHtml($html,array(
'orientation' => 'portrait',
'enable-javascript' => true,
'javascript-delay' => 1000,
'no-stop-slow-scripts' => true,
'no-background' => false,
'encoding' => 'utf-8',
'lowquality' => false,
'page-width' => '8cm',
'page-height' => '12.40cm',
'margin-left'=>0,
'margin-right'=>0,
'margin-top'=>0,
'margin-bottom'=>0,
'images' => true,
'cookie' => array(),
'dpi' => 300,
'enable-external-links' => true,
'enable-internal-links' => true,
)
);
return new Response($pdf,200,array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="recu-'.$newDate.'.pdf"'
));
} catch (Exception $e){
dump($e);
}
}
在我的 Symfony 4
应用程序上,我有一个用于使用 knp snappy bundle
从 html 生成 pdf 的按钮,但生成的 pdf 显示在同一页面中,所以我寻找一种在新选项卡上打开生成的 pdf 的方法,有没有办法做到这一点?
提前谢谢你。
你能post代码吗?
但是您应该添加 target="_blank",其中打开 pdf 的位置如下:
<a target="_blank" href="http://your_url.html">Link to the route that generates the pdf</a>
这是我的收据操作,我创建了新的收据然后打印了它:
/**
* @Route("/receipt/{id}", name="receipt_index", methods={"GET","POST"})
* @param Bill $bill
* @return Response
* @throws Exception
*/
public function newReceipt(Bill $bill): Response
{
$receipt = new Receipt();
$date = new \DateTime('now');
$receipt->setBill($bill);
$receipt->setBillCost($bill->getCost());
$receipt->setBillDate($bill->getPrintDate());
$receipt->setBillNumber($bill->getId());
$receipt->setClientName($bill->getClient()->getFullName());
$receipt->setReceiptDate($date);
$receipt->getBill()->setStatus(true);
$binary = $this->container->getParameter('knp_snappy.pdf.binary');
$snappy = new Snappy($binary);
try {
$newDate= $date->format('Y-m-d');
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($receipt);
$entityManager->flush();
$html= $this->renderView('bill/receipt.html.twig', array(
'receipt'=>$receipt,
));
$pdf=$snappy->getOutputFromHtml($html,array(
'orientation' => 'portrait',
'enable-javascript' => true,
'javascript-delay' => 1000,
'no-stop-slow-scripts' => true,
'no-background' => false,
'encoding' => 'utf-8',
'lowquality' => false,
'page-width' => '8cm',
'page-height' => '12.40cm',
'margin-left'=>0,
'margin-right'=>0,
'margin-top'=>0,
'margin-bottom'=>0,
'images' => true,
'cookie' => array(),
'dpi' => 300,
'enable-external-links' => true,
'enable-internal-links' => true,
)
);
return new Response($pdf,200,array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="recu-'.$newDate.'.pdf"'
));
} catch (Exception $e){
dump($e);
}
}