The SnappyProvider provides integration with Snappy in Silex.
wkhtmltoimage
.wkhtmltopdf
.If you are using composer to include the SilexServiceProvider in your project, you do not need to register anything. Composer automatically adds the appropriate namespaces to the autoloader.
In case you're not using composer, you will need to register the Knp\Snappy
namespace in your autoloader yourself. Make sure you place a copy of Snappy in the vendor/snappy
directory.
$app->register(new Grom\Silex\SnappyServiceProvider(), array(
'snappy.image_binary' => '/usr/local/bin/wkhtmltoimage',
'snappy.pdf_binary' => '/usr/local/bin/wkhtmltopdf',
));
You can use both snappy.image
and snappy.pdf
the same way.
use Symfony\Component\HttpFoundation\Response;
$app->get('/image', function() use ($app) {
$url = $app['request']->get('url');
$image = $app['snappy.image']->getOutput($url);
$response = new Response($image);
$response->headers->set('Content-Type', 'image/jpeg');
return $response;
});
This will convert the given url into an image. Try /image?url=http://www.github.com
use Symfony\Component\HttpFoundation\Response;
$app->get('/pdf', function() use ($app) {
$url = $app['request']->get('url');
$pdf = $app['snappy.pdf']->getOutput($url);
$response = new Response($pdf);
$response->headers->set('Content-Type', 'application/pdf');
return $response;
});
This will convert the given url into a PDF. Try /pdf?url=http://www.github.com