PHP code example of luketowers / oc-snappypdf-plugin

1. Go to this page and download the library: Download luketowers/oc-snappypdf-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

luketowers / oc-snappypdf-plugin example snippets


 namespace MyVendor\MyPlugin\Controllers;

use File;
use Twig;
use Response;
use SnappyPDF;
use Backend\Classes\Controller;

use MyVendor\MyPlugin\Models\Example as ExampleModel;

class Examples extends Controller
{
    public function download($id)
    {
        // Load the example record
        $example = ExampleModel::find($id);

        // Load the template
        $template = File::get(plugins_path('myvendor/myplugin/views/example-record-template.htm'));

        // Render the template
        $renderedHtml = Twig::parse($template, [
            'example' => $example,
        ]);

        // Render as a PDF
        $pdf = SnappyPDF::loadHTML($renderedHtml)
            ->setOption('margin-top', 0)
            ->setOption('margin-bottom', 0)
            ->setOption('margin-left', 0)
            ->setOption('margin-right', 0)
            ->setPaper('letter')
            ->output();

        return Response::make($pdf, 200, [
            'Content-Type'        => 'application/pdf',
            'Content-Disposition' => "filename={$example->name}.pdf",
        ]);
    }
}

 namespace MyVendor\MyPlugin\Controllers;

use File;
use Twig;
use Backend;
use Response;
use SnappyPDF;
use Backend\Classes\Controller;

use MyVendor\MyPlugin\Models\Example as ExampleModel;

class Examples extends Controller
{
    public function onDownloadPDF()
    {
        $recordId = input('recordId');
        return Backend::redirect("myvendor/myplugin/examples/download/$recordId");
    }
    
    public function download($id)
    {
        // Load the example record
        $example = ExampleModel::find($id);

        // Load the template
        $template = File::get(plugins_path('myvendor/myplugin/views/example-record-template.htm'));

        // Render the template
        $renderedHtml = Twig::parse($template, [
            'example' => $example,
        ]);

        // Render as a PDF
        $pdf = SnappyPDF::loadHTML($renderedHtml)->output();

        return Response::make($pdf, 200, [
            'Content-Type'        => 'application/pdf',
            'Content-Transfer-Encoding' => 'binary',
            'Content-Disposition' => "attachment; filename=\"{$example->name}.pdf\"",
        ]);
    }
}