Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravelpdfviewer Laravel Package

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).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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"
    
  2. 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).


Implementation Patterns

Core Workflows

  1. 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'])
    
  2. 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)
    
  3. 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);
    });
    
  4. 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.

  5. Custom ViewerJS Config: Override default ViewerJS settings in config/laraview.php:

    'viewerjs' => [
        'defaultZoom' => 1.2,
        'showDownloadButton' => true,
    ],
    

Gotchas and Tips

Common Pitfalls

  1. File Permissions: Ensure storage/app/public is writable:

    chmod -R 755 storage/app/public
    

    Symlink failures may occur if permissions are insufficient.

  2. Path Resolution:

    • Use absolute paths (e.g., storage/app/public/file.pdf) or relative paths from storage/app/public.
    • Avoid hardcoding public/ in paths; use storage_link() helper if needed:
      $fullPath = storage_path('app/public/' . $relativePath);
      
  3. ViewerJS Dependencies: The package relies on ViewerJS (included via CDN by default). If offline:

    • Manually include ViewerJS assets in your layout:
      <link rel="stylesheet" href="{{ asset('vendor/viewerjs/dist/viewer.css') }}">
      <script src="{{ asset('vendor/viewerjs/dist/viewer.js') }}"></script>
      
    • Or publish assets:
      php artisan vendor:publish --tag=laraview-assets
      
  4. CORS Issues: If serving PDFs from external URLs, ensure CORS headers are configured on the origin server.

  5. Large Files: ViewerJS may struggle with very large PDFs (>50MB). Consider:

    • Compressing PDFs before upload.
    • Using a dedicated PDF viewer service (e.g., PDF.js) for heavy files.

Debugging Tips

  1. Check Published Config: Verify config/laraview.php for misconfigurations (e.g., incorrect viewerjs CDN URLs).

  2. Inspect Network Requests: Use browser dev tools to confirm:

    • The PDF file is loading (check the Network tab for 404s).
    • ViewerJS scripts/styles are loaded.
  3. 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
    

Extension Points

  1. 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.

  2. Add Buttons/Tools: Extend ViewerJS functionality by modifying the viewerjs config:

    'viewerjs' => [
        'tools' => ['print', 'download', 'zoom', 'sidebar'],
    ],
    
  3. 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
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log