ariana rose
ariana rose

Reputation: 111

How to print data by current table on page in Laravel 5?

My problems are:

  1. When I click the print button, it shows the data by text not in table.
  2. I want to print data table depends on the current table show. means here is on the current page has list of all data and select option. When I select or filter table it still print all the data. The data here is collected from database.
  3. After logged in, why does the image for next page not show?

Here is my JavaScript:

<script lang='javascript'>
    $(document).ready(function(){
        $('#printPage').click(function(){
            var data = '<input type="button" value="Print this page" onClick="window.print()">';
            data += '<div id="div_print">';
            data += $('#report').html();
            data += '</div>';

            myWindow=window.open('','','width=200,height=100');
            myWindow.innerWidth = screen.width;
            myWindow.innerHeight = screen.height;
            myWindow.screenX = 0;
            myWindow.screenY = 0;
            myWindow.document.write(data);
            myWindow.focus();
        });
    });
</script>

Blade template:

 <tbody id="result">

        @foreach($profiles as $profile)
        <tr>
            <td class="student_id" width="15%">{{$profile->student_id }}</td>
            <td class="name" width="30%">{{$profile->student_name }}</td>
            <td class="program" width="30%"> {{$profile->program }}</td>
            <td class="faculty" width="25%">{{$profile->faculty }} </td>
        </tr>
            @endforeach
        </tbody>
    </table>
    <p align="center"><button id="printPage">Print</button></p>

Upvotes: 3

Views: 13329

Answers (3)

Praveena Thavarajah
Praveena Thavarajah

Reputation: 11

function printData()
{
   var print_ = document.getElementById("table_name");
   win = window.open("");
   win.document.write(print_.outerHTML);
   win.print();
   win.close();
}

The above function will print the data of a table which are on the current page due to the window.open("") function.

Upvotes: 0

JGCW
JGCW

Reputation: 1529

Rather than doing the method I suggest that you use a PDF generation plugin such as pdf-laravel.

PDF-LARAVEL :

  • to output to a file

$router->get('/pdf/output', function() { $html = view('pdfs.example')->render();

    PDF::load($html)
        ->filename('/tmp/example1.pdf')
        ->output();

    return 'PDF saved';
});
  • Adding the functionality to your controller

    class HomeController extends Controller { private $pdf;

    public function __construct(Pdf $pdf)
    {
        $this->pdf = $pdf;
    }
    
    public function helloWorld()
    {
        $html = view('pdfs.example1')->render();
    
        return $this->pdf
            ->load($html)
            ->show();
    }
    

If you want to search for other options then go to packagist.org and search for "pdf laravel" and you will find some packages built for Laravel 5 PDF generation.

Upvotes: 2

JGCW
JGCW

Reputation: 1529

Answer to the question 2:

I want to print data table depends on the current table show. means here is on the current page has list of all data and select option. When I select or filter table it still print all the data. The data here is collected from database

If you want to filter data and display there, you have two options, either filter data form the controller or you can use jQuery Datatables for that. Link here

All you have to do is:

  1. Add the Js.

    https://cdn.datatables.net/1.10.10/css/jquery.dataTables.min.css

  2. Add the css

    http://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js

  3. Call your table.

    $(document).ready(function(){ $('#myTable').DataTable(); });

Upvotes: 1

Related Questions