0

I need some help here with OctoberCMS using DynamicPDF Plugin on front end:

Have the following October CMS Page:

title = "Dues"
url = "/account/dues"
layout = "profile"
is_hidden = 0
==
<?php
use Corp\Proj\Models\Account;
use Renatio\DynamicPDF\Classes\PDF;
use Renatio\DynamicPDF\Models\PDFTemplate;

function onInvoiceDownload()
{
    $id = post("id");
    $account = Account::find($id);

    return PDF::loadTemplate("proj:invoice", ['data' => $account])->stream();
}
?>
==
{% set account = user.account %}
<button data-request="onInvoiceDownload" data-request-data="id: {{ account.id }}"  class="btn btn-default">
    <i class="fa fa-download"></i> Download
</button>

The expected behaviour would be to download the PDF File when clicking the button, but it loads and dies silently ... doing nothing visible. Tried with ->download() and ->stream() but nothing works!

Any ideas ?

Fernando Barrocal
  • 12,584
  • 9
  • 44
  • 51

2 Answers2

1

one workaround is to create a new page dedicated to the PDF creation.

title = "PDF Dues"
url = "/account/dues/pdf/:id"
layout = "profile"
is_hidden = 0
==
<?php
use Corp\Proj\Models\Account;
use Renatio\DynamicPDF\Classes\PDF;
use Renatio\DynamicPDF\Models\PDFTemplate;

function onStart()
    {
        $id= $this->param('id');    
        $account = Account::find($id);
        return PDF::loadTemplate("proj:invoice", ['data' => $account])->stream();
    }
1

Hello Fernando Barrocal,

I noticed one mistake in your code. You have to use scope resolution operator to load template.

Your Code:

return PDF::loadTemplate("proj:invoice", ['data' => $account])->stream();

Replace With:

return PDF::loadTemplate("proj::invoice", ['data' => $account])->stream();

Hope this will helps you.

Thanks!

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57
  • The first parameter of the loadTemplate method is a string representing the templatecode. It is not a scope resolution operator. The templatecode is provided on creation of the template. The use of colons in this code is optional. If the OP gave his template a code of "proj:invoice", then this can not be the mistake. – Hugo Wijdeveld Mar 15 '18 at 16:34