Search code examples
phpyiiclistview

Export List View Data to Pdf


I am using Clist View to show data such list top post on facebook. Now there is a download option . I want to download Clist view data in pdf format . I search on google but i don't find and best example. What should i do to download data of clist view in pdf format.

List view widget:

$this->widget('zii.widgets.CListView', array(
                            'dataProvider' => $data_top_posts,
                            'id' => 'tbl-top-posts',
                            'itemView' => '_top_posts',
                            'template' => '{items}',
                            'beforeAjaxUpdate' => 'js:function(id, data){
                                    $("#" + id + " .items span.empty").html("Loading... Please wait.");
                                }',
                            'afterAjaxUpdate' => 'js:function(){
                                      setProgressBar("#tbl-top-posts");
                                      }',
                            'viewData' => array(
                                'id' => 'tbl-top-posts',),
                        ));

Solution

  • There is no build-in function for such thing. But you can make that:

    1. Use HTML template to print out your view and than pass that content to TCPDF

    Example

    $pdf->AddPage();
    
    $html = $this->renderPartial('view', ['params'], true);
    
    // output the HTML content
    $pdf->writeHTML($html, true, 0, true, 0);
    $pdf->lastPage();
    $pdf->Output('example_021.pdf', 'I');
    
    1. Make image from current view and pass that image to TCPDF.

    Example

    html2canvas(document.getElementById('area')).then(function(canvas) {
        var data = canvas.toDataURL();
    
        $.ajax({
            url: 'savePDF.php',
            type: 'post',
            data: {imageSrc: data}
        });
    });
    

    You can use any other HTML to PDF tool, not only TCPDF.


    Resources:
    TCPDF, DomPDF
    Html2Canvas