psliwa / PdfBundle

This bundle integrates Symfony2 with PHPPdf library.
http://ohey.pl
MIT License
120 stars 45 forks source link

PsPdfBundle

Build Status

This bundle integrates Symfony2 with PHPPdf library. Thanks to this bundle you can easily generate PDF or image (png, jpg) files.

Documentation of PHPPdf you can find on github (README file).

Installation

  1. Use composer. PsPdfBundle requires "minimum-stability" equals to dev. Run this command:

    composer require psliwa/pdf-bundle
  2. Register bundle in AppKernel:

      //app/AppKernel.php
      public function registerBundles()
      {
          return array(
              // ..
              new Ps\PdfBundle\PsPdfBundle(),
              // ..
          );
      }

Configuration

All options are optional.

# app/config/config.yml
ps_pdf:
    nodes_file: ~
    fonts_file: ~
    complex_attributes_file: ~
    colors_file: ~
    use_cache_in_stylesheet: ~
    cache:
      type: ~
      options: ~
    markdown_stylesheet_filepath: ~
    markdown_document_template_filepath: ~
    document_parser_type: ~

Images in source document

If you want to display image, you must provide absolute path to image file via "src" attribute of image tag. Asset Twig function dosn't work, because it converts image path to relative path according to web directory. To make using of images easier, bundle provides Twig function, that converts image logical name to real, absolute path.

Example:

<pdf>
    <dynamic-page>
        <!-- pdf_image('BundleName:image-name.extension') -->
        <img src="https://github.com/psliwa/PdfBundle/raw/master/{{ pdf_image('SymfonyWebConfiguratorBundle:blue-arrow.png') }}" />
    </dynamic-page>
</pdf>

Bundle based paths in fonts and document xml file

If you want to use custom fonts, you should create your own fonts.xml config file (default fonts filepath is PHPPdf\Resources\config\fonts.xml). To make easier defining fonts paths, bundle based paths are supported. Example:

<!-- some fonts.xml file -->
<italic src="https://github.com/psliwa/PdfBundle/raw/master/%SomeBundle:file.ttf%" /> 

"%SomeBundle:file.ttf%" will be replaced by "path/to/SomeBundle/Resources/file.ttf"

Example

// In controller
//...
use Ps\PdfBundle\Annotation\Pdf;
//...

/**
 * @Pdf()
 */
public function helloAction($name)
{
    $format = $this->get('request')->get('_format');

    return $this->render(sprintf('SomeBundle:SomeController:helloAction.%s.twig', $format), array(
        'name' => $name,
    ));
}

// in helloAction.html.twig
Hello {{ name }}!

// in helloAction.pdf.twig
<pdf>
    <dynamic-page>
        Hello {{ name }}!
    </dynamic-page>
</pdf>

Bundle automatically detects pdf format (via _format) request and create pdf document from response.

Pdf annotation has four optional properties: