Giter Club home page Giter Club logo

htmltopdfbundle's Introduction

HtmlToPdfBundle

A SF2 bundle to easily handle pdf generation and concatenation with wkhtmltopdf and pdftk This bundle only provide a php object way to g, basically it will simply execute the command. If you need some particular feature, you should create your own command abstraction class.

1- Install

Add plugin to your composer.json require:

{
    "require": {
        "jihel/html-to-pdf-bundle": "dev-master",
    }
}

or

php composer.phar require jihel/html-to-pdf-bundle:dev-master

Install packages binaries:

aptitude install wkhtmltopdf pdftk xvfb

Add bundle to your AppKernel.php

public function registerBundles()
{
    $bundles = array(
        ...
        new Jihel\Plugin\HtmlToPdfBundle\JihelPluginHtmlToPdfBundle(),
    );
}

2- Configure your config.yml

The default configuration file and explanations can be found here

3- Usage

Get the generator service jihel.plugin.html_to_pdf.generator.pdf:

  • From a controller

    /** @var \Jihel\Plugin\HtmlToPdfBundle\Generator\PdfGenerator $pdfGenerator */ $pdfGenerator = $this->get('jihel.plugin.html_to_pdf.generator.pdf');

  • From a service

    service: my.super.pdf.service: class: %my.super.mdf.service.class% arguments: { '@jihel.plugin.html_to_pdf.generator.pdf' }

The class provite two methods, one to generate a pdf from a template name and datas, and the other to concatenate all pages from pdf files in an array list or a folder.

/**
 * Create a .pdf file from the given $template name with given $data parameters
 *
 * @param string $template
 * @param array $data
 * @return \SplFileObject
 */
public function create($template, array $data = array())

/**
 * Concatenate the given pdf files.
 * If you provide a path a folder,
 * concatenate them by alphabetic order
 *
 * @param \SplFileObject[]|string $files
 * @param bool $unlink
 * @return \SplFileObject
 * @throws Exception\NotASplFileObjectException
 */
public function concatenate($files, $unlink = false)

Both will return an SplFileObject temporally saved in the /tmp folder.

You can read execution messages with the methods :

public function getLastConcatenateCommandResult()
public function getLastCreateCommandResult()

Only the last executed command output will be visible. Think about the verbose option to have more details.

4- Note

The use_xvfb option is resources consuming. Think about using wkhtmltopdf on a server with a permanent xserver running.

If you want to include background pictures, put the absolute path in your $data array.

5- Thanks

Thanks to Romain Sebille (Give me your email man !) who made the research and the first iteration of the command generator. Thanks to my cat to keep meowing me. Thanks to me for giving my free time doing class for lazy developers. You can access read CV here

htmltopdfbundle's People

Contributors

jihell avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.