Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

64 lines (43 sloc) 2.269 kB

Snappy Service Provider for Silex

The SnappyProvider provides integration with Snappy in Silex.

Parameters

  • snappy.image_binary: Absolute path to wkhtmltoimage.
  • snappy.image_options: Array of options to give to Snappy (see wkhtmltoimage doc).
  • snappy.pdf_binary: Absolute path to wkhtmltopdf.
  • snappy.pdf_options: Array of options to give to Snappy (see wkhtmltopdf doc).

Services

  • snappy.image: Snappy service to create image snapshots / thumbnails.
  • snappy.pdf: Snappy service to create pdf.

Registering

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',
));

Usage

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

Jump to Line
Something went wrong with that request. Please try again.