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
Install Nutrient Web SDK into your existing Laravel project.
Add the Nutrient Web SDK (
@nutrient-sdk/viewer
) dependency:Terminal window npm i @nutrient-sdk/viewerTerminal window pnpm add @nutrient-sdk/viewerTerminal window yarn add @nutrient-sdk/viewerMake the Nutrient Web SDK assets available to your Laravel app in your
/public/assets/
directory. They can be copied as part of the build process by adding the following lines to yourwebpack.config.mix
file:mix.copy("./node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer.js","./public/assets/nutrient/nutrient-viewer.js",);mix.copy("./node_modules/@nutrient-sdk/viewer/dist/nutrient-viewer-lib","./public/assets/nutrient/nutrient-viewer-lib",);Make sure your server has the
Content-Type: application/wasm MIME
typeset. Read more about this in the troubleshooting(opens in a new tab) section of our guides.
Render a PDF
Rename the PDF document you want to display in your application to
document.pdf
, and place it in thepublic
directory. You can use this demo document(opens in a new tab) as an example.In
resources/views/welcome.blade.php
, add an empty<div>
element with a definedheight
to where Nutrient will be mounted:<div id="nutrient" style="width: 100%; height: 100vh"></div>Include
nutrient-viewer.js
on thewelcome.blade.php
page:<script src="assets/nutrient-viewer.js"></script>Initialize Nutrient Web SDK in Laravel by calling
NutrientViewer.load()
:<script>NutrientViewer.load({container: "#nutrient",document: "document.pdf" // Add the path to your document here.}).then(function(instance) {console.log("Nutrient loaded", instance);}).catch(function(error) {console.error(error.message);});</script>Run
php artisan serve
. You should see the PDF rendered in the browser.