How to export PDF from back-end edit/update view using DynamicPDF plugin in OctoberCMS

480 Views Asked by At

I am going to use DynamicPDF plugin to export to pdf some fields from backend on update/edit view of my plugin in OctoberCMS, can someone help me?

on plugin controller i have this call:

<?php namespace Vimagem\Pacientes\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Renatio\DynamicPDF\Classes\PDF;
use Renatio\DynamicPDF\Classes\PDFWrapper;


class Pacientes extends Controller
{
    public $implement = [        'Backend\Behaviors\ListController',        'Backend\Behaviors\FormController',        'Backend\Behaviors\ReorderController'    ];

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $reorderConfig = 'config_reorder.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Vimagem.Pacientes', 'main-menu-item');
    }


    /** PDF **/




public function pdf($id)
{

    return PDF::loadTemplate('export-data-pdf')->stream('download.pdf');
}



}

On the PDF Template (export-data-pdf) i need to call some form fields from one client:

{{ name }}
{{ address }}
{{ phone }}
etc...

but i can´t get the fields show up, what its wrong ?

Thank you, Vitor

2

There are 2 best solutions below

2
Pettis Brandon On

This code was found in the plugins documents.

use Renatio\DynamicPDF\Classes\PDF; // import facade

...

public function pdf()
{
    $templateCode = 'renatio::invoice'; // unique code of the template
    $data = ['name' => 'John Doe']; // optional data used in template

    return PDF::loadTemplate($templateCode, $data)->stream('download.pdf');
}

I have used this plugin and it works well. You need to pass in data to the PDF stream.

0
azvm On

This is done, worked around a solution for this.

Here is the controller application:

<?php namespace Vimagem\Pacientes\Controllers;

use Backend\Classes\Controller;
use BackendMenu;
use Renatio\DynamicPDF\Classes\PDFWrapper;
use Vimagem\Pacientes\Models\Paciente;
use \October\Rain\Database\Traits\Validation;
use Str;

class Pacientes extends Controller
{
    public $implement = [        'Backend\Behaviors\ListController',        'Backend\Behaviors\FormController',        'Backend\Behaviors\ReorderController'    ];

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $reorderConfig = 'config_reorder.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Vimagem.Pacientes', 'main-menu-item');
    }




/**** PDF Export ***/

public function pdf($id)
    {
        $paciente = Paciente::find($id);
        if ($paciente === null) {
            throw new ApplicationException('User not found.');
        }


        $filename = Str::slug($paciente->nome) . '.pdf';

        try {
            /** @var PDFWrapper $pdf */
            $pdf = app('dynamicpdf');

            $options = [
                'logOutputFile' => storage_path('temp/log.htm'),
            ];

            return $pdf
                ->loadTemplate('export-data-pdf', compact('paciente'))
                ->setOptions($options)
                ->stream($filename);

        } catch (Exception $e) {
            throw new ApplicationException($e->getMessage());
        }
    }   


}


Now i can use partials on the template like this:

<p>{{ paciente.nome }}</p>

<p>{{ paciente.morada }}</p>


etc...

Thank you all that try to helped me.

Vitor