Add PDF functionality with Laravel
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.
Add 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.pspdfkit.com/pspdfkit-web@1.10.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.pspdfkit.com/pspdfkit-web@1.10.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);});Note: Adding
useCDN: truewill load the SDK assets from the CDN ifbaseUrlisn’t provided. This flag was introduced in version 1.9.0. If your setup relies on the previous behavior of autodetecting the assets withoutbaseUrl, you can omit this flag for now, but be aware that this behavior is deprecated. In future versions, loading from CDN will become a default whenbaseUrlisn’t explicitly provided.useCDN: truehas no effect ifbaseUrlis set.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.
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.