Last active
December 30, 2018 10:09
-
-
Save treetop1500/42b92539e09609525daedd0242a99b0a to your computer and use it in GitHub Desktop.
KNP Snappy wkhtmltopdf configuration for Platform.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file means nothing. It's just here to give the Gist a name I can understand. | |
# This configuration requires the KNP Snappy Bundle [https://github.com/KnpLabs/KnpSnappyBundle] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#.platform.app.yml | |
... | |
dependencies: | |
... | |
ruby: | |
"wkhtmltopdf-binary": "> 0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config.yml | |
... | |
knp_snappy: | |
pdf: | |
enabled: true | |
binary: wkhtmltopdf | |
options: [] | |
image: | |
enabled: true | |
binary: wkhtmltoimage | |
options: [] | |
# In order for this to work locally, you'll need to install WKHTMLTOPDF (I used Brew: http://brewformulas.org/Wkhtmltopdf) and add an alias to these binaries in your .bashrc or .zshrc file: | |
# alias wkhtmltopdf='/usr/local/bin/wkhtmltopdf' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace MyBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
class ReportController extends Controller | |
{ | |
public function reportAction(Request $request, $id) { | |
$html = $this->renderView('::report.html.twig', array()); | |
... | |
$snappy = $this->get('knp_snappy.pdf'); | |
$snappy->setOption('user-style-sheet',$request->server->get('DOCUMENT_ROOT').'/css/app.css'); | |
$snappy->setOption('allow', array('/images','/css')); | |
return new Response( | |
$snappy->getOutputFromHtml($html, array('cookie' => array($session->getName() => $session->getId()))), | |
200, | |
array( | |
'Content-Type' => 'application/pdf', | |
'Content-Disposition' => 'attachment; filename="report.pdf"' | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment