Skip to content

Instantly share code, notes, and snippets.

@lucacastelnuovo
Last active October 18, 2024 11:16
Show Gist options
  • Save lucacastelnuovo/b75d08999373cfc65a8241277d0383a0 to your computer and use it in GitHub Desktop.
Save lucacastelnuovo/b75d08999373cfc65a8241277d0383a0 to your computer and use it in GitHub Desktop.
Filament V3 - Print Pop-Up
<?php
declare(strict_types=1);
namespace App\Filament\Actions;
use Closure;
use Filament\Actions\Action;
use Livewire\Component as Livewire;
final class PrintAction extends Action
{
protected Closure|string $route;
protected Closure|string|null $title = null;
public static function getDefaultName(): ?string
{
return 'print';
}
public function route(Closure|string $route): self
{
$this->route = $route;
return $this;
}
public function getRoute()
{
return $this->evaluate($this->route);
}
public function title(Closure|string $title): self
{
$this->title = $title;
return $this;
}
public function getTitle()
{
return $this->evaluate($this->title);
}
protected function setUp(): void
{
parent::setUp();
$this
->label('Afdrukken')
->icon('heroicon-o-printer')
->action(function (Livewire $livewire) {
$livewire->js(
<<<JS
let iframe = document.createElement('iframe');
let orginalTitle = document.title;
let title = "{$this->getTitle()}";
if (title) {
document.title = title;
}
iframe.src = '{$this->getRoute()}';
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.onload = function () {
iframe.contentWindow.print();
setTimeout(() => {
document.body.removeChild(iframe);
document.title = orginalTitle;
}, 1000);
};
JS
);
});
}
}
<?php
namespace App\Filament\AppPanel\Resources;
use App\Filament\AppPanel\Resources\ExampleResource\Pages;
use Filament\Resources\Resource;
// ...
class ExampleResource extends Resource
{
// ...
public static function getPages(): array
{
return [
// ...
'export' => Pages\ExportExample::route('/{record}/export'),
];
}
}
<?php
namespace App\Filament\AppPanel\Resources\ExampleResource\Pages;
use App\Filament\AppPanel\Resources\ExampleResource;
use Filament\Resources\Pages\Concerns\InteractsWithRecord;
use Filament\Resources\Pages\Page;
class ExportExample extends Page
{
use InteractsWithRecord;
protected static string $resource = ExampleResource::class;
protected static string $layout = 'components.layouts.pdf';
protected static string $view = 'pdf.example';
public function mount(int|string $record): void
{
$this->record = $this->resolveRecord($record);
}
}
<?php
namespace App\Filament\AppPanel\Resources\ExampleResource\Pages;
use App\Filament\Actions\PrintAction;
use App\Filament\AppPanel\Resources\ExampleResource;
use Filament\Actions\EditAction;
use Filament\Resources\Pages\ViewRecord;
class ViewExample extends ViewRecord
{
protected static string $resource = ExampleResource::class;
protected function getHeaderActions(): array
{
return [
PrintAction::make()
->title('hello_world')
->route(
fn () => ExportExample::getUrl(['record' => $this->record])
),
];
}
}

Filament V3 - Print Pop-Up

This code allows you to show a pop-up to print a "PDF" (rendered html) without first having to open the PDF viewer and requiring the user to press the print button.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{ config('app.name', 'Laravel') }}</title>
<script src="https://cdn.tailwindcss.com"></script>
<style type="text/css" media="print">
@page {
size: A4;
margin: 0;
}
</style>
</head>
<body class="antialiased overflow-hidden">
{{ $slot }}
</body>
</html>
<div>
<section class="pb-8 max-w-xl mx-auto">
<h1>Hello World!</h1>
<p>This is page 1</p>
</section>
@pageBreak
<section class="pb-8 max-w-xl mx-auto">
<h1>Foo Bar</h1>
<p>This is page 2</p>
</section>
@pageBreak
</div>
@lucacastelnuovo
Copy link
Author

lucacastelnuovo commented Oct 12, 2024

I forgot to say we also use spatie/laravel-pdf in some other parts of the app so @pageBreak refers to the following code:

Blade::directive('pageBreak', function () {
    return "<?php echo '<div style=\"page-break-after: always;\"></div>'; ?>";
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment