Reputation: 663
I want to print the content of a Laravel Blade file that contains an HTML table of users. When the user clicks the print button, it should open a print dialog in a pop-up window. Please help in resolving this issue.
Currently, I have code to read the file's content, but I don't know how to specify the path of this file. The file is located at resources/views/reports/table.blade.php.
Current code shows me the exception:
FileNotFoundException in Filesystem.php line 41: File does not exist at path printview_table
Code in Controller
public function printfile()
{
$filename = '/reports/printview_table.blade.php';
try
{
$contents = File::get($filename);
printfile($contents);
}
catch (Illuminate\Filesystem\FileNotFoundException $exception)
{
die("The file doesn't exist");
}
}
Upvotes: 4
Views: 22007
Reputation: 452
I know it's too late to answer but I did the same thing today and decided to post my solution here as well so that it might help someone in the future.
Required Tools:
Solution Steps:
Step 1: The view where the print button is located in.
dashboard.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Document</title>
</head>
<body>
<button id="print"> Print </button>
<script src="{{ asset('js/jquery.js') }}"></script>
<script src="{{ asset('js/jquery.print.js') }}"></script>
<script src="{{ asset('js/scripts.js') }}"></script>
</body>
</html>
Step 2: The view that is going to be printed.
Assume I have a view like this and I want to print this out:
print.blade.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<table>
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
<th>Col 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Cell 1</td>
<td>Cell 2</td>
<td>Cell 3</td>
</tr>
</tbody>
</table>
</body>
</html>
Step 2: The route.
We need a route to call our view through that.
web.php
Route::post('/print', function() { return view('print'); });
Step 3: The jQuery script.
We are going to call the view content by AJAX and pass it to the jQuery.print()
function for printing.
scripts.js
$('#print').on('click', function() {
let CSRF_TOKEN = $('meta[name="csrf-token"').attr('content');
$.ajaxSetup({
url: '/print/',
type: 'POST',
data: {
_token: CSRF_TOKEN,
},
beforeSend: function() {
console.log('printing ...');
},
complete: function() {
console.log('printed!');
}
});
$.ajax({
success: function(viewContent) {
$.print(viewContent); // This is where the script calls the printer to print the viwe's content.
}
});
});
That's all, customize the codes according to your own requirements.
Upvotes: 4
Reputation: 105
Why don't you just pass this task to JS. In the view add a btn and on the click event call the print func.
window.print();
https://www.w3schools.com/jsref/met_win_print.asp
Upvotes: -1