on my Symfony 4
app, i have a botton that used to generate pdf from html using knp snappy bundle
but the generated pdf get displayed in the same page , so i'm looking for a way to open the generated pdf on new tab, is there a way to accomplish that ?
Thank you in advance .
can you post the code?
however you should add the target="_blank" where the pdf is opened like this:
<a target="_blank" href="http://your_url.html">Link to the route that generates the pdf</a>
here is my receipt action , i created creat new receipt and then i print it :
/**
* @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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.