Convert Figma logo to code with AI

KnpLabs logosnappy

PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. Wrapper for wkhtmltopdf/wkhtmltoimage

4,377
434
4,377
8

Top Related Projects

Convert HTML to PDF using Webkit (QtWebKit)

Laravel Snappy PDF

10,446

HTML to PDF converter for PHP

4,147

Official clone of PHP library to generate PDF documents and barcodes

4,355

PHP library generating PDF files from UTF-8 encoded HTML

7,206

A pure PHP library for reading and writing word processing documents

Quick Overview

Snappy is a PHP library that provides a simple and efficient way to generate PDF and image files from HTML or URLs. It acts as a wrapper for the powerful wkhtmltopdf and wkhtmltoimage command-line tools, making it easy to integrate PDF and image generation capabilities into PHP applications.

Pros

  • Easy to use API for generating PDFs and images from HTML or URLs
  • Supports both synchronous and asynchronous generation
  • Integrates well with popular PHP frameworks like Symfony and Laravel
  • Actively maintained with regular updates and bug fixes

Cons

  • Requires wkhtmltopdf/wkhtmltoimage to be installed on the server
  • Limited customization options compared to more complex PDF libraries
  • May have performance issues with very large or complex HTML documents
  • Depends on external binaries, which can be a security concern in some environments

Code Examples

  1. Generate a PDF from HTML:
use Knp\Snappy\Pdf;

$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Hello, World!</h1>', '/path/to/output.pdf');
  1. Generate an image from a URL:
use Knp\Snappy\Image;

$snappy = new Image('/path/to/wkhtmltoimage');
$snappy->generate('https://example.com', '/path/to/output.jpg');
  1. Generate a PDF with custom options:
use Knp\Snappy\Pdf;

$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->setOption('margin-top', 10);
$snappy->setOption('margin-bottom', 10);
$snappy->generateFromHtml('<h1>Custom Margins</h1>', '/path/to/output.pdf');

Getting Started

  1. Install Snappy via Composer:
composer require knplabs/knp-snappy
  1. Ensure wkhtmltopdf/wkhtmltoimage is installed on your server.

  2. Basic usage in PHP:

use Knp\Snappy\Pdf;

$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Hello, Snappy!</h1>', 'output.pdf');

This will generate a PDF file named 'output.pdf' in the current directory with the content "Hello, Snappy!" as an H1 heading.

Competitor Comparisons

Convert HTML to PDF using Webkit (QtWebKit)

Pros of wkhtmltopdf

  • Standalone command-line tool, can be used independently of any programming language
  • More direct control over PDF generation process
  • Supports a wider range of HTML and CSS features for rendering

Cons of wkhtmltopdf

  • Requires separate installation and management of the wkhtmltopdf binary
  • Less integrated with PHP applications compared to Snappy
  • May have more complex setup and configuration process

Code Comparison

wkhtmltopdf (command-line usage):

wkhtmltopdf http://example.com output.pdf

Snappy (PHP integration):

use Knp\Snappy\Pdf;

$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Hello World</h1>', 'output.pdf');

wkhtmltopdf is a standalone tool that can be used directly from the command line, offering more flexibility but requiring separate installation. Snappy, on the other hand, provides a PHP wrapper around wkhtmltopdf, making it easier to integrate into PHP applications but with potentially less direct control over the PDF generation process.

Laravel Snappy PDF

Pros of laravel-snappy

  • Seamless integration with Laravel framework
  • Provides Laravel-specific facades and configuration
  • Easier to use within Laravel projects due to built-in service provider

Cons of laravel-snappy

  • Limited to Laravel ecosystem
  • Depends on KnpLabs/snappy as a core dependency
  • May have slower update cycles compared to the core Snappy library

Code Comparison

laravel-snappy:

use Barryvdh\Snappy\Facades\SnappyPdf;

$pdf = SnappyPdf::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');

snappy:

use Knp\Snappy\Pdf;

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf');

The laravel-snappy example demonstrates the use of Laravel facades and integration with Laravel's view system, while the snappy example shows direct usage of the library without Laravel-specific features.

Both libraries serve the purpose of generating PDFs, with laravel-snappy providing a more Laravel-friendly approach and snappy offering a framework-agnostic solution. The choice between them depends on whether the project is Laravel-based and if the additional Laravel integration is beneficial for the specific use case.

10,446

HTML to PDF converter for PHP

Pros of dompdf

  • Pure PHP solution, no external dependencies required
  • Easier to set up and use in PHP environments
  • Better support for CSS styling and layout

Cons of dompdf

  • Slower performance for large or complex documents
  • Limited support for advanced PDF features
  • May struggle with certain fonts or complex layouts

Code Comparison

dompdf:

require_once 'dompdf/autoload.inc.php';
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream("document.pdf");

Snappy:

use Knp\Snappy\Pdf;
$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml($html, '/path/to/output.pdf');

Key Differences

  • dompdf is a pure PHP solution, while Snappy relies on wkhtmltopdf
  • Snappy generally offers better performance and rendering quality
  • dompdf provides more extensive CSS support
  • Snappy has better support for JavaScript and complex layouts
  • dompdf is easier to set up in PHP-only environments
  • Snappy requires additional system dependencies

Use Cases

  • Choose dompdf for simpler PDF generation tasks in PHP-only environments
  • Opt for Snappy when dealing with complex layouts, JavaScript, or high-volume PDF generation
4,147

Official clone of PHP library to generate PDF documents and barcodes

Pros of TCPDF

  • Pure PHP implementation, no external dependencies required
  • Extensive feature set for complex PDF generation
  • Supports a wide range of fonts and encodings

Cons of TCPDF

  • Steeper learning curve due to its complexity
  • Can be slower for simple PDF generation tasks
  • Larger codebase, which may impact performance

Code Comparison

TCPDF:

$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
$pdf->Cell(0, 10, 'Hello, World!', 0, 1);
$pdf->Output('example.pdf', 'F');

Snappy:

$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Hello, World!</h1>', 'example.pdf');

Key Differences

Snappy is a wrapper for wkhtmltopdf, which converts HTML to PDF using WebKit. It's simpler to use for basic PDF generation from HTML content. TCPDF, on the other hand, is a more comprehensive PDF library that offers greater control over PDF creation and manipulation but requires more code for basic tasks.

Snappy is ideal for quickly generating PDFs from existing HTML, while TCPDF is better suited for creating complex, customized PDF documents from scratch or when fine-grained control over PDF elements is needed.

4,355

PHP library generating PDF files from UTF-8 encoded HTML

Pros of mpdf

  • Native PHP implementation, no external dependencies required
  • Extensive support for CSS and HTML, including advanced features like tables and forms
  • Ability to generate PDFs from HTML strings or files

Cons of mpdf

  • Slower performance compared to Snappy, especially for large documents
  • Limited support for JavaScript and dynamic content
  • May produce larger file sizes for complex documents

Code Comparison

mpdf:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello World</h1>');
$mpdf->Output('document.pdf', 'F');

Snappy:

$snappy = new Knp\Snappy\Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Hello World</h1>', 'document.pdf');

Both libraries offer straightforward ways to generate PDFs from HTML. mpdf uses a native PHP approach, while Snappy relies on the wkhtmltopdf binary.

mpdf provides more fine-grained control over PDF generation and supports a wider range of HTML and CSS features. However, Snappy leverages the power of wkhtmltopdf, which can offer better performance and more accurate rendering of complex web pages.

The choice between these libraries depends on specific project requirements, such as performance needs, deployment constraints, and the complexity of the HTML content being converted to PDF.

7,206

A pure PHP library for reading and writing word processing documents

Pros of PHPWord

  • Offers more comprehensive document creation and manipulation capabilities
  • Supports a wider range of document formats (DOCX, RTF, ODT, HTML)
  • Provides extensive formatting options for text, paragraphs, and styles

Cons of PHPWord

  • Steeper learning curve due to more complex API
  • Larger library size and potentially higher resource usage
  • May be overkill for simple document generation tasks

Code Comparison

PHPWord:

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->addText('Hello World!');
$writer = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$writer->save('helloWorld.docx');

Snappy:

use Knp\Snappy\Pdf;
$snappy = new Pdf('/path/to/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Hello World</h1>', 'helloWorld.pdf');

PHPWord is more suited for creating complex Word documents with advanced formatting, while Snappy excels at generating PDFs from HTML content. PHPWord offers greater control over document structure and content, but Snappy provides a simpler API for quick PDF generation from existing HTML.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Snappy

Build Status AppVeyor CI Build Status Scrutinizer Code Quality

Snappy is a PHP library allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopdf and wkhtmltoimage available on OSX, linux, windows.

You will have to download wkhtmltopdf 0.12.x in order to use Snappy.

Please, check FAQ before opening a new issue. Snappy is a tiny wrapper around wkhtmltox, so lots of issues are already answered, resolved or wkhtmltox ones.

Following integrations are available:

Installation using Composer

composer require knplabs/knp-snappy

Usage

Initialization

<?php

require __DIR__ . '/vendor/autoload.php';

use Knp\Snappy\Pdf;

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');

// or you can do it in two steps
$snappy = new Pdf();
$snappy->setBinary('/usr/local/bin/wkhtmltopdf');

Display the pdf in the browser

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
echo $snappy->getOutput('http://www.github.com');

Download the pdf from the browser

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.github.com');

Merge multiple urls into one pdf

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput(array('http://www.github.com','http://www.knplabs.com','http://www.php.net'));

Generate local pdf file

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$snappy->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf');

Pass options to snappy

// Type wkhtmltopdf -H to see the list of options
$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$snappy->setOption('disable-javascript', true);
$snappy->setOption('no-background', true);
$snappy->setOption('allow', array('/path1', '/path2'));
$snappy->setOption('cookie', array('key' => 'value', 'key2' => 'value2'));
$snappy->setOption('post', array('key' => 'value'));
$snappy->setOption('cover', 'pathToCover.html');
// .. or pass a cover as html
$snappy->setOption('cover', '<h1>Bill cover</h1>');
$snappy->setOption('toc', true);
$snappy->setOption('cache-dir', '/path/to/cache/dir');

Reset options

Options can be reset to their initial values with resetOptions() method.

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
// Set some options
$snappy->setOption('copies' => 4);
// ..
// Reset options
$snappy->resetOptions();

wkhtmltopdf binary as composer dependencies

If you want to download wkhtmltopdf and wkhtmltoimage with composer you add to composer.json:

composer require h4cc/wkhtmltopdf-i386 0.12.x
composer require h4cc/wkhtmltoimage-i386 0.12.x

or this if you are in 64 bit based system:

composer require h4cc/wkhtmltopdf-amd64 0.12.x
composer require h4cc/wkhtmltoimage-amd64 0.12.x

And then you can use it

<?php

use Knp\Snappy\Pdf;

$myProjectDirectory = '/path/to/my/project';

$snappy = new Pdf($myProjectDirectory . '/vendor/h4cc/wkhtmltopdf-i386/bin/wkhtmltopdf-i386');

// or

$snappy = new Pdf($myProjectDirectory . '/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');

N.B. These static binaries are extracted from Debian7 packages, so it might not be compatible with non-debian based linux distros

Some use cases

If you want to generate table of contents and you want to use custom XSL stylesheet, do the following:

<?php
$snappy = new Pdf('/path/to/binary');

$snappy->setOption('toc', true);
$snappy->setOption('xsl-style-sheet', 'http://path/to/stylesheet.xsl') //or local file;

$snappy->generateFromHtml('<p>Some content</p>', 'test.pdf');

Bugs & Support

If you found a bug please fill a detailed issue with all the following points. If you need some help, please at least provide a complete reproducer so we could help you based on facts rather than assumptions.

  • OS and its version
  • Wkhtmltopdf, its version and how you installed it
  • A complete reproducer with relevant php and html/css/js code

If your reproducer is big, please try to shrink it. It will help everyone to narrow the bug.

Maintainers

KNPLabs is looking for maintainers (see why).

If you are interested, feel free to open a PR to ask to be added as a maintainer.

We’ll be glad to hear from you :)

Credits

Snappy has been originally developed by the KnpLabs team.