Generate pdf from a custom class with cakephp 3

32 Views Asked by At

I am using the CakePdf plugin for cakephp version 3.2 but I have a doubt according to the documentation and its general usage is like this

class Reports extends AppController
{

    public function initialize() {
        $this->loadComponent('RequestHandler');
        $this->_validViewOptions[] = 'pdfConfig';
    }

    public function test()
    {

        $this->pdfConfig = [
               'margin' => [
                            'top' => 17,
                            'bottom' => 20,
                            'left' => 20,
                            'right'=> 20
                        ],
               'orientantion' => 'portrait',
               'filename' => 'Report.pdf',
               'download' => true
            ];
    }
}

The previous example works and it is normal, now I have the need to use the plugin within a class that is not a controller which will be in charge of generating the pdf.

use CakePdf\Pdf\CakePdf;

class ReportAnalytical
{

    private $_defaultConfig = [
        'margin' => [
            'top' => 17,
            'bottom' => 20,
            'left' => 20,
            'right'=> 20
        ],
        'filename' => 'Test',
        'orientantion' => 'portrait',
        'download' => true
    ];

    private $cakePdf;

    public function __construct()
    {
        $this->cakePdf = new CakePdf($this->_defaultConfig);
    }

    public function downloadPdfSpanish()
    {

    }
}

What is not clear to me is how do I do the same thing I did in the controller but in this class so that its downloadPdfSpanish method can be downloaded directly when the pdf is called from the service in the controller, which would be as follows

class ReportAnalyticalPdfService
{

    public function __construct()
    {
        $this->reportPdf = new ReportAnalytical();
    }

    public function responsePdfAnalitycalSpanish()
    {
        $this->reportPdf->downloadPdfSpanish();
    }

}

call from controller

class ReportsGroupsController extends AppController
{

    public function test()
    {
        $response = $this->reportAnalyticalPdf->responsePdfAnalitycalSpanish();
        return $response;
    }

}

the architecture that I am applying is ddd with the framework

0

There are 0 best solutions below