how can I change pdf from datatable for pdf for serverside html2pdf?

433 views Asked by At

I want to change my button pdf from buttons datatable for my pdf from server, but in the same position how can I do it? my url from html2pdf http://localhost/store_websocket/inventory/pdf and I want to change the button from pdfmaker from datatable in the position for my url for html2pdf because I want researching datatables fails with 1k or more rows then I decide server side procesing

controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PDF extends MY_Controller {

    public function __construct(){
        parent::__construct();
    }

     public function index(){
        require_once(APPPATH.'third_party/html2pdf/vendor/autoload.php'); 
        $data['products'] = $this->products->datatable(); 
        //print_r($data);
        $template_pdf = $this->load->view('view_pdf', $data, TRUE);
        $html2pdf = new HTML2PDF('P', 'A4', 'en');
        $html2pdf->WriteHTML($template_pdf);
        $html2pdf->Output('exemple.pdf');
    }

}

view

<style type="text/css">

P {text-align:justify;font-size: 12pt;}
li {text-align:justify;font-size: 12pt;}
table.page_footer {width: 100%; border: none; border-top: solid 1px #000000; }
table.products {border-collapse: collapse; width: 100%;}
table.products th,td  {text-align: left; padding: 8px;}
table.products thead tr{font-size: 9.5pt}
table.products th {background-color: #34495e;  color: white;}
tbody tr {background-color: #f2f2f2;}
</style>

<page backtop="14mm" backbottom="14mm" backleft="10mm" backright="10mm" style="font-size: 12pt">
    <page_footer>
        <table class="page_footer">
            <tr>
                <td style="width: 100%; text-align: right">
                    page [[page_cu]]/[[page_nb]]
                </td>
            </tr>
        </table>
    </page_footer>
<div style="width:100%;text-align:center;">
    <h1><b>List Inventory</b></h1>
</div>
   <table class="products">
      <thead>
          <tr>
              <th>Codigo</th>
              <th>Descripcion</th>
              <th>Precio compra</th>
              <th>Precio venta</th>
              <th>Precio mayoreo</th>
              <th>Existencia</th>
          </tr>
      </thead>
      <tbody>
        <?php foreach ($products as $key): ?>
            <tr>
              <td><?php echo $key['id']?></td>
              <td><?php echo $key['descripcion']?></td>
              <td><?php echo $key['precio_compra']?></td>
              <td><?php echo $key['precio_venta']?></td>
              <td><?php echo $key['precio_mayoreo']?></td>
              <td><?php echo $key['existencia']?></td>
            </tr>
        <?php endforeach ?>
      </tbody>
   </table>
</page>

AJAX

$(function(){
    URL_GET_DATATABLE = BASE_URL+'inventory/product/datatable';
    URL_GET_VIEW_PRODUCT = BASE_URL+'inventory/product/getViewProduct'; 
    URL_GET_ADD_PRODUCT = BASE_URL+'inventory/product/addProduct';
    var table = $('#example').DataTable({
        "lengthChange": false,
        responsive: true,
        dom: 'Blfrtip',
        buttons: [{
             extend: 'excelHtml5',
             exportOptions:{
                columns: [1,2,3,4,5,6]
             }
        },{
            extend: 'csvHtml5',
            exportOptions:{
                columns: [1,2,3,4,5,6]
            }
        },{
            extend: 'pdf',
            exportOptions: {
                columns: [1,2,3,4,5,6]
            }
        }],
        ajax: {
            url: URL_GET_DATATABLE,
            type: 'POST',
        },
        columnDefs:[{
            targets: -1,
            data: null,
            defaultContent: "<a href='#'><span class='glyphicon glyphicon-pencil'></span></a>"

        },{
            targets: 6,
            render: function (data) {
                return (data == 1) ? "<span class='label label-success'>active</span>":"<span class='label label-danger'>inactive</span>";
            }
        }],
        fnRowCallback: function (data,nRow) {
            if (nRow[6] == 0) {
                $(data).css({'background-color':'#f2dede'})
            }else if(nRow[6] == 1){
                $(data).css({'background-color':'#dff0d8'})
            }else{

            }
        }
    });

       $('#example tbody').on('click','a', function(e){
            alert('thing');
        });

        $('#add').on('click',function(){
            $("#description").mask("(999) 999-9999");
            $("#new_product").validate();
            BootstrapDialog.show({
                type: BootstrapDialog.TYPE_PRIMARY,
                message: function(dialog) { 
                    var $message = $('<div></div>');
                    var pageToLoad = dialog.getData('pageToLoad');
                    $message.load(pageToLoad);

                    return $message;
                },
                data: {
                    'pageToLoad': URL_GET_VIEW_PRODUCT
                },
                closable: false,
                buttons:[{
                    id: 'btn-ok',
                    cssClass: 'btn-primary',
                    icon: 'glyphicon glyphicon-send',
                    label: ' Save',
                    action: function (e) {
                        var description = $('#description').val();
                        var cost_price = $('#cost_price').val();
                        var selling_price = $('#selling_price').val();
                        var wprice = $('#wprice').val();
                        var min_stock = $('#min_stock').val();
                        var stock = $('#stock').val();
                        var max_stock = $('#max_stock').val();
                        if($("#new_product").valid()){
                            $.ajax({
                                url: URL_GET_ADD_PRODUCT,
                                type: 'POST',
                                data: {description: description, cost_price: cost_price, selling_price: selling_price, wprice: wprice, min_stock: min_stock, stock: stock, max_stock: max_stock}
                            }).done(function (data) {
                                console.log(data);
                                if (data.msg == 'successfully added') {
                                    $('#new_product')[0].reset();
                                    table.ajax.reload();
                                }else if(data.min_stock == 'el stock no puede ser mayor al min'){
                                    BootstrapDialog.show({
                                        type: BootstrapDialog.TYPE_WARNING,
                                        message: 'el stock no puede ser mayor al min'
                                    });
                                }
                            });
                            return false;
                        }
                    }
                },{
                    id: 'btn-cancel',
                    cssClass: 'btn-danger',
                    icon: 'glyphicon glyphicon-remove',
                    label: ' Cancel',
                    action: function (e) {
                        e.close();
                    }
                }]
            });
        });
    });
0

There are 0 answers