Drag-and-drop PDF uploads with instant preview in javascript
TL;DR: This guide shows you how to build a simple drag-and-drop tool in JavaScript that lets users upload PDF files and instantly see a preview—just like modern file uploaders. It walks you through using plain HTML, JavaScript, and the browser’s built-in PDF preview tools, so there’s no need for heavy libraries. You’ll learn how to create a clean user experience with just a few lines of code, making it easy to add this feature to any web app. Perfect for developers who want a fast and simple way to let users upload and view PDFs.
PDF is the go-to format for handling user-accessible documents in web applications. From receipts for small online purchases to multi-million-dollar contracts, PDFs are everywhere!
Since the web runs on JavaScript, most apps handle basic PDF operations through JS tools spanning multiple libraries. But once you start implementing advanced features like drag-and-drop PDF uploads, instant previews, and editing, things can quickly get... less fun for developers and users.
To offer a seamless, modern drag-and-drop PDF upload and preview experience for users of your SaaS infrastructure, consider using Joyfill’s fillable PDF SDK and document API as shown in the following example.
Step 1: Create a JavaScript project
Open a terminal and run the following command to create a new JavaScript project using Vite.
The above command will scaffold a vanilla JS project named pdf-upload. Navigate into the project folder and install its build tools by running the following commands.
Step 2: Generate a Joyfill Access Token
To implement drag-and-drop PDF upload and instant preview, we’ll need a Joyfill API access token. Complete the following steps to generate one:
Sign up for a Joyfill account if you don’t already have one.
In the Users dashboard of your Joyfill account, click the Access Tokens button to the right of your account information.
Click Add Access Token.
Copy the newly generated access token and set it as the value of the VITE_JOYFILL_ACCESS_TOKEN variable in the .env file at the root of the pdf-upload folder.
Step 3: Import Joyfill components from a CDN
Add the following <script> tag to the head element of the project’s index.html file. It will import Joyfill’s components into the application.
Note that @joyfill/components can also be installed as an NPM package. However, for convenience, this example loads it through a CDN.
Step 4: Set Joyfill PDF form builder’s mount point
Add a div element to the body of the application’s index.html file and set “joyfill” as the value of the div’s id attribute.
Step 5: Configure ‘Joyfill’ as an external library
Create a vite.config.js file at the root of the project’s folder and add the below configuration to it.
Step 6: Define helper functions to upload/preview PDF documents to/from Joyfill’s API
Create an src/upload-pdf.js file and add the following script to it.
Step 7: Initialize Joyfill’s PDF form builder
Copy the following into the src/main.js file to initialize Joyfill.
Step 8: Build and preview the application
Open a terminal and navigate to the pdf-upload folder. Then, run the following commands.
Now, visit the URL displayed in the terminal (e.g., http://localhost:4173) to view the application in your browser.
Step 9: Drag and drop a PDF File to upload and preview it
Drag a PDF file into the file upload section around the top-right of the page and drop it to upload the document.
Fig. File upload section of Joyfill’s PDF form editor’s file upload button

Wait a few seconds (depending on your network speed) for the document preview to load.
That’s it! Now you can place elements from the form builder’s toolbar(s) over the document preview to edit it.
Need to build PDF form capabilities inside your SaaS application? Joyfill makes it easy for developers to natively build and embed form and PDF experiences inside their own SaaS applications.