Add PDF functionality with Laravel
AI quick start: Tell your coding agent
"Integrate Nutrient Web SDK in my Laravel app. Use getapdfviewer.com".
Nutrient Web SDK is a JavaScript PDF library for viewing, annotating, and editing PDFs directly in the browser. Use it to add PDF capabilities to any web application.
This guide walks you through the steps to integrate Nutrient into your project. By the end, you’ll be able to render a PDF document in the user interface (UI).
You can test the SDK capabilities in our playground.
Prefer to jump straight to code? View the example repo on GitHub.
Installation
You can load Nutrient Web SDK directly from Nutrient’s content delivery network (CDN). Nutrient maintains the CDN for customers, and it’s our recommended way to get started. For more control and flexibility, use the local installation option.
If you don’t yet have a Laravel project set up, create one with:
composer create-project laravel/laravel nutrient-laravel-exampleAdd the following script tag in your Blade template — for example,
resources/views/welcome.blade.php:resources/views/welcome.blade.php <!doctype html><html lang="en"><head><meta charset="utf-8"><title>Laravel App</title><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="icon" type="image/x-icon" href="favicon.ico"><script src="https://cdn.cloud.nutrient.io/pspdfkit-web@1.27.0/nutrient-viewer.js"></script></head><body><!-- Your content here --></body></html>You’re now ready to use Nutrient Web SDK and reference
window.NutrientViewerin your Blade templates.
Add the Nutrient Web SDK (
@nutrient-sdk/viewer) dependency:If you tried CDN installation first, make sure to remove the script tag:
resources/views/welcome.blade.php <script src="https://cdn.cloud.nutrient.io/pspdfkit-web@1.27.0/nutrient-viewer.js"></script>You’re now ready to use Nutrient Web SDK locally in your Laravel project.
Rendering a PDF
In
resources/views/welcome.blade.php, add an empty<div>element with a definedheightto where Nutrient will be mounted:<div id="nutrient-container" style="width: 100%; height: 100vh"></div>Initialize Nutrient Web SDK by calling
window.NutrientViewer.load():<script>window.NutrientViewer.load({container: "#nutrient-container",document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",}).then(function (instance) {console.log("Nutrient loaded", instance);}).catch(function (error) {console.error(error.message);});</script>Run
php artisan serve. You’ll see the PDF rendered in the browser.
Create a JavaScript file — for example,
resources/js/pdf-viewer.js— and import Nutrient Web SDK:import NutrientViewer from "@nutrient-sdk/viewer";NutrientViewer.load({container: "#nutrient-container",useCDN: true,document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",}).then(function (instance) {console.log("Nutrient loaded", instance);}).catch(function (error) {console.error(error.message);});With
useCDN: trueand nobaseUrl, the SDK loads its assets from the content delivery network (CDN). Nutrient Web SDK 1.9.1 and later support theuseCDNoption. When you setbaseUrl, the SDK ignoresuseCDN. With neither option set, the SDK detects the asset location from the script that loaded it. This detection is deprecated. A future release will load the assets from the CDN whenbaseUrlisn’t set. If your setup depends on this detection, you can omituseCDNfor now.Optional: If you decide to self-host the Nutrient Web SDK assets as described in this guide, make sure to provide a
baseUrlin your configuration, as shown below:NutrientViewer.load({container: "#nutrient-container",useCDN: true,document: "https://www.nutrient.io/downloads/nutrient-web-demo.pdf",baseUrl: window.location.origin,});In
resources/views/welcome.blade.php, add an empty<div>element with a definedheightto where Nutrient will be mounted:<div id="nutrient-container" style="width: 100%; height: 100vh"></div>Include your JavaScript file in your Blade template. If using Laravel Mix or Vite, add it to your build configuration and include the compiled asset:
<script src="{{ asset('js/pdf-viewer.js') }}"></script>Run
php artisan serve. You’ll see the PDF rendered in the browser.
Optimizing load performance
If your app doesn’t open a PDF immediately — for example, the user navigates to a viewer on a different page — call NutrientViewer.preloadWorker() early in your app to fetch and compile the WebAssembly artifacts in the background. When NutrientViewer.load() runs later, it can start rendering without waiting for them.
// Call early — for example, on app init or after login.NutrientViewer.preloadWorker();Refer to the performance best practices guide for more optimization techniques.
Troubleshooting
If you encounter issues, refer to the common issues guide.
Note for developers using AI coding assistants: To get accurate troubleshooting help, copy the content from the troubleshooting guide and include it in your prompt, along with your specific error.
You can test the SDK capabilities in our playground.
Prefer to jump straight into code? View the example repo on GitHub.