Open PDFs without Downloading Using JavaScript

Due to the nature of the web, opening a PDF document in the browser involves sending its data over to the user’s browser, which makes it impossible to fully prevent the user from downloading it.

However, there are a few ways PSPDFKit for Web can make this more difficult, so as to discourage users who wish to do this.

Try for Free Launch Demo

Disabling Exporting

You can prevent a user from using the export toolbar item to download your PDF by omitting it from the list of toolbar items provided in the configuration:

instance.setToolbarItems(items => items.filter((item) => item.type !== "export-pdf");

To read more about the download/export button, see our Download/Export Button guide.

Watermarking

You can use text annotations to render text in a PDF with a user’s details — such as their username — to discourage sharing of your document with other customers:

// Create a free text annotation.
const textAnnotation = new PSPDFKit.Annotations.TextAnnotation({
  boundingBox: new PSPDFKit.Geometry.Rect({
    left: 228,
    top: 924,
    width: 600,
    height: 80
  }),
  fontSize: 40,
  text: {
    format: "plain",
    value: `Generated For ${username}`,
  },
  pageIndex: 0
});

// Add the annotations to the document.
await instance.create(textAnnotation);

Using a Password

To prevent unauthorized exporting of your documents, PSPDFKit for Web supports the protection of documents with a password. When saving your PDF from your PDF authoring tool, set a password for it.

Server-Side Rendering

PSPDFKit for Web Server-Backed instances will use the Server to stream the pages it’s looking at as images instead of streaming the complete PDF document to the user’s browser.

Learn more about the PSPDFKit for Web Server-Backed operational mode.