joaopaulolndev/filament-pdf-viewer
FilamentPHP PDF viewer for displaying documents in your admin UI. Adds a PdfViewerField for forms and an infolist entry to preview PDFs from database records or direct paths/URLs. Compatible with Laravel 11 and Filament 5.x.
Installation
Update your composer.json to require the new version with Filament 5.3+:
composer require joaopaulolndev/filament-pdf-viewer:^3.0
Publish the config (if needed):
php artisan vendor:publish --provider="JoaopauloLndev\FilamentPdfViewer\FilamentPdfViewerServiceProvider"
Basic Setup
Register the package in your AppServiceProvider or a dedicated service provider:
FilamentPdfViewer::register();
Note: Ensure your filament/filament package is updated to ^5.3 or higher.
First Use Case: Displaying a PDF in a Resource
Add the HasPdfViewer trait to your resource class:
use JoaopauloLndev\FilamentPdfViewer\Traits\HasPdfViewer;
class MyResource extends Resource
{
use HasPdfViewer;
// ...
}
Define the PDF column in your table:
public static function table(Table $table): Table
{
$table
->columns([
// ...
Tables\Columns\PdfViewerColumn::make('pdf_column_name'),
]);
}
Configuring the Viewer
Check the published config at config/filament-pdf-viewer.php for default settings like:
Embedding PDFs in Tables
Use PdfViewerColumn for inline preview:
PdfViewerColumn::make('document')
->width('100%')
->height('500px')
->embed()
->label('Document Preview'),
Standalone PDF Viewer in Pages
Create a custom page with the viewer (ensure Filament ^5.3 compatibility):
use JoaopauloLndev\FilamentPdfViewer\Widgets\PdfViewer;
class DocumentPage extends Page
{
public function mount()
{
$this->pdfData = $this->getPdfData(); // Fetch PDF from DB/storage
}
public function getPdfViewer(): PdfViewer
{
return PdfViewer::make()
->data($this->pdfData)
->width('full')
->height('600px');
}
}
Dynamic PDF Sources Fetch PDFs from:
Storage::disk('public')->url($record->pdf_path)).->allowExternalUrls(true)).->data()).Integration with Forms
Use PdfViewer in a form tab for uploads/previews:
use JoaopauloLndev\FilamentPdfViewer\Widgets\PdfViewer;
PdfViewer::make('pdf_preview')
->data($this->record->pdf_data)
->hiddenLabel()
->required(false),
Customizing the Viewer Extend the default viewer with JS/CSS:
PdfViewer::make()
->extraAttributes(['class' => 'custom-pdf-viewer'])
->extraJs('
document.addEventListener("DOMContentLoaded", function() {
console.log("PDF viewer loaded!");
});
'),
Batch Actions Add a bulk PDF download/action:
use JoaopauloLndev\FilamentPdfViewer\Actions\DownloadPdfAction;
public static function table(Table $table): Table
{
$table
->actions([
DownloadPdfAction::make(),
]);
}
Caching PDFs Cache rendered PDFs for performance:
use JoaopauloLndev\FilamentPdfViewer\Facades\PdfViewer;
$cachedPdf = PdfViewer::cache($pdfData, 'pdf_cache_key', now()->addHours(1));
Localization Override labels in your resource:
PdfViewerColumn::make('document')
->label(__('filament-pdf-viewer::pdf.preview')),
Filament Version Mismatch
Class 'Filament\Resources\Pages\PageConfiguration' not foundfilament/filament to ^5.3 or higher:
composer require filament/filament:^5.3
CORS Issues with External URLs
->allowExternalUrls(true) but validate URLs server-side to avoid XSS.Large PDFs Breaking Layouts
width/height to prevent overflow:
->width('800px')->height('600px')
->responsive() for adaptive sizing.Base64 Data Size Limits
$compressedData = gzcompress($base64Data);
Permission Errors
chmod -R 755 storage/app/public
->storageDisk('s3') for cloud storage.Check Console for Errors
F12) to debug PDF.js loading issues.Log PDF Data
\Log::debug('PDF Data:', ['data' => $pdfData]);
Disable Embed for Testing
->embed(false) to debug raw PDF rendering.Clear Cache
php artisan cache:clear
php artisan view:clear
Custom PDF.js Config
Override default PDF.js settings in config/filament-pdf-viewer.php:
'pdfjs' => [
'cMapUrl' => 'custom/path/to/cmap/',
'cMapPacked' => true,
],
Add Annotations/Tools Extend the viewer with custom buttons:
->extraJs('
PDFViewerApplication.options = {
...PDFViewerApplication.options,
secondaryToolbarButtons: [
{ id: "custom-button", label: "Custom Tool", click: () => alert("Clicked!") }
]
};
')
Hook into PDF Events
Use JavaScript events (e.g., pagesinit, pagechange) for custom logic:
->extraJs('
document.getElementById("pdf-viewer").addEventListener("pagesinit", function() {
console.log("Pages loaded!");
});
')
Proxy External PDFs For security, proxy external URLs through your backend:
// In your controller
public function proxyPdf($url)
{
return response()->streamDownload(
file_get_contents($url),
'document.pdf'
);
}
Then use the proxied URL in the viewer.
Fallback for Unsupported Browsers Provide a download link as fallback:
PdfViewerColumn::make('document')
->fallbackAction(
Actions\Action::make('Download PDF')
->url(fn ($record) => route('download.pdf', $record))
),
How can I help you explore Laravel packages today?