PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] Cakephp 3 - CakePDF and Search Plugin - render filtered data to PDF?

 January 16, 2022     cakephp, pdf, php     No comments   

Issue

I'm using the FriendsOfCake CakePDF Plugin with wkhtmltopdf to render my views as PDF. I also use their Search plugin to filter view data with a form.

For now, when I print the data in the view it always renders all view data into the PDF and not only the filtered data that is displayed on screen.

Is there any way to do this? I can't find anything that mentions such a case in the Plugin Docs. It seems like the PDF plugin always reloads the page in its default state or rather loads the default query from the index function instead of the filtered data. Since this is my first CakePDF project I don't really get what I have to do to make it render the filtered data instead. Can anybody help with that?

Here is what my main files look like so far:

class PaintingsController extends AppController
{
    public function index()
   {
    $query = $this->Paintings
        ->find('search', 
            $this->Paintings->filterParams($this->request->query))
        ->contain(['Artists', 
                    'Tickets' => function ($q) {
                            return $q->where(['Tickets.active' => false]);
                     }
                ]);

    $this->viewBuilder()->options([
        'pdfConfig' => [
            'orientation' => 'portrait',
            'filename' => 'paintings.pdf'
        ]
    ]);

    $this->set('paintings', $this->paginate($query));
    $this->set('_serialize', ['paintings']);
    }
}
class PaintingsTable extends Table
{
    public function searchConfiguration()
    {
    $search = new Manager($this);

    $search->like('title', [
                'before' => true,
                'after' => true,
                'field' => $this->aliasField('title'),
                'filterEmpty' => true
        ])->value('property', [
            'field' => $this->aliasField('property'),
            'filterEmpty' => true
        ])->like('artist_name', [
            'before' => false,
            'after' => true,
            'field' => $this->Artists->target()->aliasField('surname'),
            'filterEmpty' => true
        ])->value('technique', [
            'field' => $this->aliasField('technique'),
            'filterEmpty' => true
        ]);

      return $search;

    }
}
In Template\Paintings\index.ctp

... data in tables ...

<?= $this->Html->link('Save as PDF',[
                         'action' => 'index',
                         '_ext' => 'pdf'],[
                               'class' => 'create-pdf-link', 
                               'target' => 'blank'
                     ]) ?>

Then everything gets rendered in Templates\Paintings\pdf\index.ctp without the applied filtering.


Solution

Your PDF link won't contain any filter paramters, so there is no reload or anything, it just won't do any filtering.

The current query is not being incorportated automatically when generating links/URLs, you have to explicitly pass it to the URL array on your own, like

$this->Html->link(
    'Save as PDF',
    [
        'action' => 'index',
        '_ext' => 'pdf'
    ] + $this->request->query, // there it goes
    [
        'class' => 'create-pdf-link',
        'target' => 'blank'
    ]
);

See also Cookbook > Routing > Generating URLs



Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing