Coder Social home page Coder Social logo

laravelchromepdf's Introduction

Laravel PHP Chrome HTML to PDF

Important note regarding git

If you keep you vendor folder in source control - then you may want to consider placing the following in you .gitignore file. The reason for this is that the chromium rendering engine contains files that are larger than GitHubs 100mb limit.

/vendor/spiritix/php-chrome-html2pdf/node_modules/*

How it works

This library is based on puppeteer, a headless Chrome Node API which is maintained by the Chrome DevTools team.

It provides a simple PHP / Laravel wrapper around the Node API, focused on generating beautiful PDF files.

In contrast to other HTML to PDF converters like wkhtmltopdf, the corresponding PHP wrapper or similar libraries, it is based on a current Chrome version instead of outdated and unmaintained WebKit builds. This library therefore fully supports CSS3, HTML5, SVGs, SPAs, and all the other fancy stuff people use these days.

Installation

composer require chrisbraybrooke/laravel-chrome-pdf

Setup:

Laravel >=5.5

Laravel 5.5 and above uses package autodiscovery so you are all done! Skip to Usage.

Laravel 5.5<

If you are using Laravel 5.4 or below, you will have to manually register the package. After updating composer, add the ServiceProvider to the providers array in config/app.php.

ChrisBraybrooke\LaravelChromePdf\ServiceProvider::class,

And optionally add the Facade.

'ChromePDF' => ChrisBraybrooke\LaravelChromePdf\ChromePDF::class,

Usage:

Below is an example of creating a simple PDF from a blade file.

namespace App\Http\Controllers;

use App\Invoice;
use ChromePDF;

class InvoicesController {

    /**
     * Download a PDF version of the invoice.
     *
     * @return void
     */
    public function show(Invoice $invoice)
    {
        // Load resources/views/invoice.blade.php
        ChromePDF::loadView('invoice', ['invoice' => $invoice])
            ->size('a4')
            ->landscape()
            ->download("invoice-{$invoice->ref}.pdf");
    }
}

Outputs:

There are several methods of outputting the PDF.

Of course download is available.

ChromePDF::loadView('invoice', ['invoice' => $invoice])->download("invoice-{$ref}.pdf");

Use inline to show the PDF inline in the browser.

ChromePDF::loadView('invoice', ['invoice' => $invoice])->inline();

Or save to save the file to the filesystem. The first argument is the filename / path - and the second is the disk to be used.

ChromePDF::loadView('invoice', ['invoice' => $invoice])->save("invoice-{$ref}.pdf", 's3');

Options:

It is simple to set options for the PDF.

Just use the setOption or setOptions methods.

ChromePDF::loadHtml('<h1>Hello world</h1>')->setOption('scale', '0.2')->download('hello.pdf');

Or set multiple options at once.

ChromePDF::loadHtml('<h1>More options here!</h1>')
    ->setOptions(['scale' => 0.2, 'landscape'])
    ->download('options.pdf');

All available php-chrome-html2pdf options are available.

There are also a few helper methods, which can be chained.

ChromePDF::loadHtml('<h1>Hello world</h1>')
    // a3 & a5 also available - pass true to set as landscape. Or use size('') and specify a different page size.
    ->a4()
    // Set the orientation as landscape - default is portrait.
    ->landscape()
    // Load a blade template for the page headers.
    ->headerView('default-header')
    // Load a blade template for the page footers.
    ->footerView('default-footer')
    ->download();

Maintenance:

My company Purple Mountain - A web development company in the UK, will try our best to keep this package up to date and free from any issues.

laravelchromepdf's People

Stargazers

 avatar Nikhil Bhatia avatar John Zhang avatar pramitmarattha avatar Akash Yadav avatar Jake Causier avatar

Watchers

James Cloos avatar Christian Braybrooke avatar

laravelchromepdf's Issues

Shell Error 127

I'm getting a shell error 127 in my live environment when using your package.

In the troubleshooting section of the https://github.com/spiritix/php-chrome-html2pdf:

Manually set the path to your Node.js executable using the $converter->setNodePath() method

How can I do that via your package? Is there a method?

support pageWaitFor

Hello I am having some trouble with your package compared to doing a manual print to pdf in a chrome browser. I believe this is because what I'm rendering is a VUE component and some things are not rendered by time the download() is called.

Here is a screenshot of when I print to PDF in browser:

image

Here is a screenshot of when I print to PDF using your package.

image

The only work around I can think of is using php-chrome-html2pdf's pageWaitFor method to set a delay. But your wrapper doesn't seem to expose that.

I know it may be weird to use vue for something like this but we have a pre-existing component for a web view that is quite appropriate to include in our PDF going to customers so I would like to make use of the existing component.

setOption printBackground not being respected

I don't know what I'm doing wrong or if it's a limitation with your package but attempting to print background images does not seem to be working.

ChromePDF::loadView('quotes.show', $data)
            ->setOption('printBackground', True)
            ->download("{$quote->quote_number}.pdf");

All of your examples of setOption use loadHtml not loadView. I'm not thinking this matters just calling it out in case there is some actual difference.

Should this work as is?

No Laravel 6.0 Support

Problem 1
- Conclusion: remove chrisbraybrooke/laravel-chrome-pdf 0.0.5
- Conclusion: don't install laravel/framework v6.0.3
- Conclusion: don't install laravel/framework v6.0.2
- Installation request for chrisbraybrooke/laravel-chrome-pdf ^0.0.5 -> satisfiable by chrisbraybrooke/laravel-chrome-pdf[0.0.5].
- Conclusion: don't install laravel/framework v6.0.1

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.