Giter Club home page Giter Club logo

html-to-pdf-laravel's Introduction

City Curator Report

HTML to PDF convertor for cc reports.

Laravel-Snappy installation

  1. Require this package in your composer.json and update composer.
$ composer require barryvdh/laravel-snappy
  1. After updating composer, add the ServiceProvider to the providers array in config/app.php
Barryvdh\Snappy\ServiceProvider::class,
  1. Use the Facade for shorter code. Add this to your facades:
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
'SnappyImage' => Barryvdh\Snappy\Facades\SnappyImage::class,
  1. Finally you can publish the config file:
$ php artisan vendor:publish --provider="Barryvdh\Snappy\ServiceProvider"
  1. Require wkhtmltopdf package in your composer.json and update composer.
LINUX 
$ composer require h4cc/wkhtmltopdf-amd64 0.12.x
WINDOWS
composer require wemersonjanuario/wkhtmltopdf-windows "0.12.2.3"
  1. The main change to this config file (config/snappy.php) will be the path to the binaries.
LINUX
'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
WINDOWS
'binary' => base_path('vendor/wemersonjanuario/wkhtmltopdf-windows/bin/64bit/wkhtmltopdf.exe'),
  1. Create reports directory in your resources/views and add this files there
'footer.blade.php' -> footer for all report pages
'header.blade.php', -> header for all report pages
'report.blade.php', -> all report pages
  1. Add css files to public/css and js files to public/js
css files => [app.css, fonts.css]
js files => [chart.js, doughnot.js]
  1. Set this options in config/snappy.php
'pdf' => [
    enabled' => true,
    'binary' => base_path('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'),
    'timeout' => false,
    'options' => [
        'enable-javascript' => true,
        'javascript-delay' => 1000,
        'enable-smart-shrinking' => true,
        'no-stop-slow-scripts' => true,
        'margin-top' => 15,
        'margin-bottom' => 12,
        'margin-left' => 0,
        'margin-right' => 0,
    ],
    'env' => [],
],
  1. Finally, add this code to your report controller
public function report()
{
    $viewName = 'reports.report';
    $header = View::make('reports.header');
    // for some reason $footer is not working (need to check this)
    // $footer = View::make('reports.footer');
    $pdf = PDF::loadView($viewName)->setOrientation('landscape');
    $pdf->setOption('enable-local-file-access', true);
    $pdf->setOption('header-html', $header);
    // $pdf->setOption('footer-html', $footer);
        
    // download pdf file
    return $pdf->download('report.pdf');

    //stream pdf file
    // return $pdf->download('report.pdf');

    //open pdf as view
    // return view($viewName);
}

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.