问题
I`m using Symfony2 kpn snappy bundle to generate pdfs. I want to generate PDF from an html page with css. I found a solution, but it has a problem with :
$pageUrl = $this->generateUrl('accounts_management_generate_pdf_markup',
array('invoice' => $invoiceData), true); // use absolute path!
return new \Symfony\Component\HttpFoundation\Response(
$this->get('knp_snappy.pdf')->getOutput($pageUrl), 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
The problem is that the pageUrl accounts_management_generate_pdf_markup
is behind a security area and cannot be accessed without authentication. The generated file is just the login page, to which this path accounts_management_generate_pdf_markup
redirects if not logged.
My questions are:
Is there any way to pass to snappy authentication credentials?
Is there another way using snappy bundle to generate the pdf using styles(css)
回答1:
You can add the session cookie as an argument to the getOutput function:
$pageUrl = $this->generateUrl('route', array('id' => $id), true);
$session = $this->get('session');
$session->save();
session_write_close();
return new Response(
$this->get('knp_snappy.pdf')->getOutput($pageUrl, array('cookie' => array($session->getName() => $session->getId()))),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
来源:https://stackoverflow.com/questions/20286012/symfony-2-kpn-snappy-generate-pdf-with-output-meets-security-area