goodnesskay/laravelpdfviewer
Embed and view PDFs seamlessly in Laravel using ViewerJS. Install via Composer, publish the laraview assets, then load any public PDF in an iframe using a simple asset URL (e.g., /laraview/#../pdf/file.pdf).
Installation:
composer require goodnesskay/laravelpdfviewer
Register the service provider in config/app.php:
Goodnesskay\LaravelPdfViewer\LaravelPdfViewerServiceProvider::class,
Publish the config:
php artisan vendor:publish --provider="Goodnesskay\LaravelPdfViewer\LaravelPdfViewerServiceProvider"
First Use Case:
Place a PDF file (e.g., document.pdf) in your storage/app/public directory.
In a Blade template, embed the viewer:
@pdfViewer('document.pdf')
Ensure the file is accessible via a symbolic link or URL (e.g., /storage/document.pdf).
Embedding PDFs in Views:
Use @pdfViewer('path/to/file.pdf') in Blade templates. The package dynamically generates a ViewerJS-powered viewer.
Example:
@pdfViewer('documents/report.pdf', ['width' => '800px', 'height' => '600px'])
Dynamic PDF Paths: Pass Eloquent model attributes or dynamic paths:
$pdfPath = $document->file_path; // e.g., 'uploads/invoices/invoice_123.pdf'
return view('documents.show', compact('pdfPath'));
In Blade:
@pdfViewer($pdfPath)
Integration with Controllers: Return PDFs directly from routes:
Route::get('/view-pdf/{id}', function ($id) {
$pdf = Document::find($id)->file_path;
return view('pdf.viewer')->with('pdf', $pdf);
});
Asset Management:
Store PDFs in storage/app/public and symlink to public/storage:
php artisan storage:link
Access files via /storage/path/to/file.pdf.
Custom ViewerJS Config:
Override default ViewerJS settings in config/laraview.php:
'viewerjs' => [
'defaultZoom' => 1.2,
'showDownloadButton' => true,
],
File Permissions:
Ensure storage/app/public is writable:
chmod -R 755 storage/app/public
Symlink failures may occur if permissions are insufficient.
Path Resolution:
storage/app/public/file.pdf) or relative paths from storage/app/public.public/ in paths; use storage_link() helper if needed:
$fullPath = storage_path('app/public/' . $relativePath);
ViewerJS Dependencies: The package relies on ViewerJS (included via CDN by default). If offline:
<link rel="stylesheet" href="{{ asset('vendor/viewerjs/dist/viewer.css') }}">
<script src="{{ asset('vendor/viewerjs/dist/viewer.js') }}"></script>
php artisan vendor:publish --tag=laraview-assets
CORS Issues: If serving PDFs from external URLs, ensure CORS headers are configured on the origin server.
Large Files: ViewerJS may struggle with very large PDFs (>50MB). Consider:
Check Published Config:
Verify config/laraview.php for misconfigurations (e.g., incorrect viewerjs CDN URLs).
Inspect Network Requests: Use browser dev tools to confirm:
Network tab for 404s).Fallback for Missing Files: Handle missing PDFs gracefully in Blade:
@if(file_exists(storage_path('app/public/' . $pdfPath)))
@pdfViewer($pdfPath)
@else
<p class="text-danger">PDF not found.</p>
@endif
Custom Viewer Templates: Override the default viewer template by publishing and modifying:
php artisan vendor:publish --tag=laraview-views
Edit resources/views/vendor/laraview/viewer.blade.php.
Add Buttons/Tools:
Extend ViewerJS functionality by modifying the viewerjs config:
'viewerjs' => [
'tools' => ['print', 'download', 'zoom', 'sidebar'],
],
Integration with Storage Disks:
For files stored on S3 or other disks, use the Storage facade to generate URLs:
$url = Storage::disk('s3')->url($pdfPath);
@pdfViewer($url) // Pass the full URL
How can I help you explore Laravel packages today?