Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save treetop1500/42b92539e09609525daedd0242a99b0a to your computer and use it in GitHub Desktop.
Save treetop1500/42b92539e09609525daedd0242a99b0a to your computer and use it in GitHub Desktop.
KNP Snappy wkhtmltopdf configuration for Platform.sh
# 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]
#.platform.app.yml
...
dependencies:
...
ruby:
"wkhtmltopdf-binary": "> 0.0"
#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'
<?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