uga/twig2pdf-bundle

twig2pdf

Installs: 272 484

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 2

Forks: 5

Open Issues: 1

Type:symfony-bundle

v1.3 2017-02-26 12:44 UTC

This package is auto-updated.

Last update: 2024-04-24 22:24:38 UTC


README

logo.png?raw=true

Twig2pdf Bundle

Scrutinizer Code Quality Minimum PHP Version License Build Status

Twig2pdf for Symfony 2/Drupal 8 as a service.

 _______        _____ ____ _  _   ____  ____  _____ 
|_   _\ \      / /_ _/ ___| || | |  _ \|  _ \|  ___|
  | |  \ \ /\ / / | | |  _| || |_| |_) | | | | |_   
  | |   \ V  V /  | | |_| |__   _|  __/| |_| |  _|  
  |_|    \_/\_/  |___\____|  |_| |_|   |____/|_|    
                                                    
 ____  _   _ _   _ ____  _     _____   ______   __
| __ )| | | | \ | |  _ \| |   | ____| | __ ) \ / /
|  _ \| | | |  \| | | | | |   |  _|   |  _ \\ V / 
| |_) | |_| | |\  | |_| | |___| |___  | |_) || |  
|____/ \___/|_| \_|____/|_____|_____| |____/ |_|  
                                                  
 ____  _        _    _   _ _____ _____ ____  ____  _______     __
|  _ \| |      / \  | \ | | ____|_   _/ __ \|  _ \| ____\ \   / /
| |_) | |     / _ \ |  \| |  _|   | |/ / _` | | | |  _|  \ \ / / 
|  __/| |___ / ___ \| |\  | |___  | | | (_| | |_| | |___  \ V /  
|_|   |_____/_/   \_\_| \_|_____| |_|\ \__,_|____/|_____|  \_/   
                                      \____/                     

How to install ?

Just add this to your composer.json file:

"require": {
  "uga/twig2pdf-bundle": "^1.1"
}

Or you tape in your ROOT project 
composer require uga/twig2pdf-bundle

Enable it in the Kernel

new UGA\Html2PDFBundle\UGAHtml2PDFBundle() , 

How to make it ?

In your controller:

    protected $pdf2html  ; 

    public function __construct(
                \UGA\Html2PDFBundle\Factory\Html2pdfFactory $pdf2html 
            )
    {
        $this->pdf2html         = $pdf2html ; 
    }

In your action:

    $html2pdf =  $this->pdf2html->getInstance() ;
    $html2pdf->WriteHTML($template);
    $html2pdf->Output('exemple.pdf');

You can pass every option you would pass to twig2pdf, for instance :

$html2pdf = $this->pdf2html->getInstance('P', 'A4', 'en', true, 'UTF-8', array(10, 15, 10, 15)); or by container
$html2pdf =  $this->container->get('uga_html2_pdf.service')->getInstance('P', 'A4', 'fr', null, null, null);

If the previous arguments are not provided, the factory uses its own default values. You can change this default values by adding the bundle configuration to your app/config/config.yml :

uga_html2_pdf:
    html2_pdf_orientation: 'P'
    html2_pdf_format: 'A4'
    html2_pdf_lang: 'fr'
    html2_pdf_unicode: true
    html2_pdf_encoding: 'UTF-8'
    html2_pdf_margin: [10,15,10,15]

Integration with twig template :

    $template = $this->template->render('AcmeMyAppBundle:Default:index.html.twig',array(
        'CLASS_HTML2PDF' => "HTML2PDF FOR SYMFONY 2.8.18 "
    )) ;  
    

    $html2pdf =  $this->pdf2html->getInstance() ;
    $html2pdf->WriteHTML($template);
    $html2pdf->Output('exemple.pdf');

Full example ( Controller ) :

    <?php

namespace Acme\MyAppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DefaultController extends Controller
{
    
    protected $entityManager  ; 
    protected $template  ; 
    protected $pdf2html  ; 

    public function __construct(
                \Doctrine\ORM\EntityManager $entityManager , 
                \Twig_Environment $template  , 
                \UGA\Html2PDFBundle\Factory\Html2pdfFactory $pdf2html 
            )
    {
        $this->entityManager    = $entityManager;
        $this->template         = $template ; 
        $this->pdf2html         = $pdf2html ; 
    }
    
    public function indexAction()
    {

    $template = $this->template->render('AcmeMyAppBundle:Default:index.html.twig',array(
        'CLASS_HTML2PDF' => "CLASS HTML2PDF FOR SYMFONY 2.8.18 "
    )) ;  
    

    $html2pdf =  $this->pdf2html->getInstance() ;
    $html2pdf->WriteHTML($template);
    $html2pdf->Output('exemple.pdf');
    
        return $this->render('AcmeMyAppBundle:Default:index.html.twig');
    }
}

Update package in vendor project ?

composer install

Information:

Contact Me

Nadir Fouka < nadir@fouka.ovh >

  • Web Developer Grenoble Alpes University 2017
  • Data Scientist Planet@Dev