Developed with love by KnpLabs Hire us for your project!
36

DompdfBundle

by core23

This bundle provides a wrapper for using dompdf inside symfony.

DompdfBundle

Latest Stable Version
Latest Unstable Version
License

Total Downloads
Monthly Downloads
Daily Downloads

Continuous Integration
Code Coverage

This bundle provides a wrapper for using dompdf inside symfony.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

composer require core23/dompdf-bundle

Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    Core23\DompdfBundle\Core23DompdfBundle => ['all' => true],
];

Usage

Whenever you need to turn a html page into a PDF use dependency injection for your service:

final class MyService 
{
    public function __construct(DompdfFactoryInterface $factory) 
    {
        $this->factory = $factory;
    }

    public function render()
    {
        // ...
        $this->factory->create();
        // ...
    }
}

final class MyOtherService 
{
    public function __construct(DompdfWrapperInterface $wrapper) 
    {
        $this->wrapper = $wrapper;
    }

    public function stream()
    {
        // ...
        $html = '<h1>Sample Title</h1><p>Lorem Ipsum</p>';

        $response = $this->wrapper->getStreamResponse($html, "document.pdf");
        $response->send();
        // ...
    }

    public function binaryContent()
    {
        // ...
        return $this->wrapper->getPdf($html);
        // ...
    }
}

If you use Twig to create the content, make sure to use renderView() instead of render().
Otherwise you might get the following HTTP header printed inside your PDF:

HTTP/1.0 200 OK Cache-Control: no-cache

$html = $this->renderView('my_pdf.html.twig', array(
    // ...
));
$this->wrapper->getStreamResponse($html, 'document.pdf');

Configure the Bundle

# config/packages/core23_dompdf.yml

core23_dompdf:
    defaults:
        dpi: 150
        defaultPaperSize: A4
        ...

Events

The dompdf wrapper dispatches events to convenient get the inner dompdf instance when creating the pdf.
- dompdf.output is dispatched in getPdf
- dompdf.stream is dispatched in streamHtml

See Symfony event dispatcher documentation for more info.

License

This bundle is under the MIT license.

  • Add funding information to composer.json (#59)
    By web-flow, 5 months ago
  • Add funding information to composer.json
    By core23, 5 months ago
  • Replace Makefile with composer scripts (#58)
    By web-flow, 8 months ago
  • Replace Makefile with composer scripts
    By core23, 8 months ago
  • Modernize test files (#56)
    By web-flow, 9 months ago
  • Modernize test files
    By core23, 9 months ago
  • Add code style settings for imports (#53)
    By web-flow, 9 months ago
  • Add code style settings for imports
    By core23, 9 months ago
  • Add new baseline for phpstan (#52)
    By web-flow, 9 months ago
  • Update git ignore
    By core23, 9 months ago
  • Rework Makefile
    By core23, 9 months ago
  • Simplify editorconfig
    By core23, 9 months ago
  • Add new baseline for phpstan
    By core23, 9 months ago
  • Enhancement: Use ergebnis/composer-normalize instead of localheinz/composer-normalize (#51)
    By web-flow, 9 months ago
  • Enhancement: Use ergebnis/composer-normalize instead of localheinz/composer-normalize
    By core23, 9 months ago
  • Update usage instruction
    By web-flow, 9 months ago
  • Prepare 2.6.0 release
    By web-flow, 9 months ago
  • Add integration test (#50)
    By web-flow, 9 months ago
  • Add integration test
    By core23, 9 months ago
  • Add mutation testing (#49)
    By web-flow, 9 months ago
  • Add mutation testing
    By core23, 9 months ago
  • Add missing strict file header (#48)
    By web-flow, 9 months ago
  • Add missing strict file header
    By core23, 9 months ago
  • Update phpstan extensions (#42)
    By web-flow, 9 months ago
  • Add missing type hints
    By core23, 9 months ago
  • Update phpstan extensions
    By core23, 9 months ago
  • Use shared probot config (#47)
    By web-flow, 9 months ago
  • Use shared probot config
    By core23, 9 months ago
  • Add symfony version lock to matrix build
    By core23, 9 months ago
  • Remove old symfony <4.2 code
    By core23, 9 months ago