Convert Javascript Embedded HTML to PDF Laravel 5.5


Here in this document we will focus on the following two things.

  • HTML to PDF.

  • HTML to PDF with Javascript embedded.

I assume that you have already done the basic setup of Lavavel 5.5.

If you haven’t done that, go to the laravel documentation and follow the steps.

https://laravel.com/docs/5.5.

HTML to PDF

Installation:

Require the following package in your composer.json and update composer.

This will download the package and the dompdf + fontlib libraries also.

 

Step1:

$ composer require barryvdh/laravel-dompdf

After updating composer, add the ServiceProvider to the providers array in config/app.php

Step2:

Barryvdh\DomPDF\ServiceProvider::class,

You can optionally use the facade for shorter code. Add this to your facades:

'PDF' => Barryvdh\DomPDF\Facade::class,

Step3:

Usage:
You can create a new DOMPDF instance and load a HTML string, file or view name.
You can save it to a file, or stream (show in browser) or download.

Step4:

Example code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App;

/**
 * @author Uday Kumar <uday@ahex.co.in>
 * @access public
 * @version 1.0.0
 */
class PDFController extends Controller
{
    public function __construct()
    {
        //
    }
    
    //-------------------------------------------------------------------------
    
    /**
     * Method to parse the HTML to PDF
     * 
     * @return string
     */
    public function loadPdf()
    {
        $pdf = App::make('dompdf.wrapper');
        $pdf->loadHTML('<h1>Welcome to Ahex</h1>');
        return $pdf->stream();
    }
}
//end of class PDFController
//end of file PDFController.php


 

For further documentation refer https://github.com/barryvdh/laravel-dompdf

 

 

HTML to PDF with Javascript embedded

Installation:

Do the following steps.

Step1:

$ composer require knplabs/knp-snappy

Step2:

$ composer require h4cc/wkhtmltopdf-i386 0.12.x

or this if you are in 64 bit based system:

$ composer require h4cc/wkhtmltopdf-amd64 0.12.x

Step3:

Example code

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Knp\Snappy\Pdf;

/**
 * @author Uday Kumar <uday@ahex.co.in>
 * @access public
 * @version 1.0.0
 */
class PDFController extends Controller
{
    public function __construct()
    {
        //
    }
    
    //-------------------------------------------------------------------------

    /**
     * Method to parse the Java script embedded HTML to PDF
     * 
     * @return string
     */
    public function loadPdfJsEmbedded()
    {
        $style = '<h1 id="tag">Welcome to Ahex</h1>'
                . '<script type="text/javascript">'
                . 'document.getElementById("tag").style.color="blue"'
                . '</script>';
       
        //get project directory path
        $projectDirectory = base_path();

        //wkhtmltopdf binary as composer dependencies if it is 64 bit based system
        $snappy = new Pdf($projectDirectory . '/vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');
        $snappy->setOption('enable-javascript', true);

        header('Content-Type: application/pdf');
        echo $snappy->getOutputFromHtml($style);
    }
}
//end of class PDFController
//end of file PDFController.php

ahex2

Thanks for reading..........have a nice day.