Coder Social home page Coder Social logo

gonzalo1987 / spraedpdfgeneratorbundle Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stedekay/spraedpdfgeneratorbundle

0.0 1.0 0.0 5.25 MB

This bundle generates a PDF (or even multiple PDFS into one) from HTML, based on the Flying Saucer project (http://code.google.com/p/flying-saucer/).

License: MIT License

PHP 97.54% HTML 2.46%

spraedpdfgeneratorbundle's Introduction

SpraedPDFGeneratorBundle

SpraedPDFGeneratorBundle generates HTML documents to PDF. The bundle gives you the chance to add a page header and footer very easily (which can be disabled/switched on the first page).

It works with a little jar library based on the [Flying Saucer project][flyingsaucer]. So you need to run Java on your server (Java 6 or later).

Total Downloads Latest Stable Version

ToDo

  • Write a little example how to enable header and footer
  • Writing tests (test frame available for UnitTests)
  • Check for possibilities to use SVG files in HTML

Installation

Option 1: Use composer

"require": {
    "php": ">=5.3.2",
    "symfony/symfony": "2.1.*",

    ... (your packages) ...

    "spraed/pdf-generator-bundle": "v1.0",
}

Option 2: Use git submodules

Copy the SpraedPDFGeneratorBundle into the vendor/bundles/Spraed/PDFGeneratorBundle directory:

git submodule add https://github.com/stedekay/SpraedPDFGeneratorBundle.git vendor/bundles/Spraed/PDFGeneratorBundle

Option 3: Use deps file

[SpraedPDFGeneratorBundle]
    git=https://github.com/stedekay/SpraedPDFGeneratorBundle.git
    target=/bundles/Spraed/PDFGeneratorBundle

Register the Spraed namespace:

$loader->registerNamespaces(array(
    ...
    'Spraed'                        => __DIR__.'/../vendor/bundles',

Finally, you can enable it in your kernel:

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        ...
        new Spraed\PDFGeneratorBundle\SpraedPDFGeneratorBundle(),
        ...

Usage

There is a service registered in the services.yml to generate pdf files. Just call the PDF generator from the service class and call the generatePDF()-method with the XHTML and the url of the PDF:

$html = $this->renderView('AcmeDemoBundle:Default:index.html.twig');
$pdfGenerator = $this->get('spraed.pdf.generator');

Also you are able to set an encoding option (you can leave the second parameter, it defaults to UTF-8):

$pdfGenerator->generatePDF($html, 'UTF-8');

Anything else will be handled by the Response object in the controller, i.e.:

    $html = $this->renderView('ACMEYourBundle:Print:print.html.twig');
    $pdfGenerator = $this->get('spraed.pdf.generator');

    return new Response($pdfGenerator->generatePDF($html),
                    200,
                    array(
                        'Content-Type' => 'application/pdf',
                        'Content-Disposition' => 'inline; filename="out.pdf"'
                    )
    );

If you wish the pdf to be offered as a download, simply change 'inline' in 'Content-Disposition' to 'attachment'.

Make sure that all assets in your HTML are linked with absolute paths, because the HTML is copied into a tmp folder on the server. If you want to add an image to your twig it should look something like this:

    {{ app.request.scheme ~'://' ~ app.request.httpHost ~ asset('images/foo.jpg') }}

You are also capable of printing multiple pdfs in one stack. Saying you generate multiple documents from multiple html files and you want to output those in on huge pdf file, there is the 'generatePDFs' method which takes an array of rendered html Views and sticks those together:

    $twigs[0] = 'SpraedSomethingBundle:Print:print_pdf_one.html.twig'
    $twigs[1] = 'SpraedSomethingBundle:Print:print_pdf_two.html.twig'

    $htmlCollection = array();
    foreach($twigs as $twig){
            $htmlCollection[] = $this->renderView($twig);
    }

    return new Response($pdfGenerator->generatePDFs($htmlCollection, 'UTF-8'),
        200,
        array(
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="out.pdf"'
        )
    );

To define proper print css you might want to read into the w3.org's hints on that: [w3.org] [w3.org]: http://www.w3.org/TR/css3-page/ [flyingsaucer]: https://github.com/flyingsaucerproject/flyingsaucer [spraed]: http://www.spraed.com

spraedpdfgeneratorbundle's People

Contributors

inmarelibero avatar kernel64 avatar nyholm avatar rodnaph avatar stedekay avatar stefanheppenheimer avatar

Watchers

 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.