CloudPDF
DocsPricing
Start building

Permissions

A document can be opened with less than full power: a reviewer who may comment but not download, a signer who may only fill in the form, a public link that only shows the pages. Each thing a user may do is a permission, a string like doc.download.

Check a permission before you show the control for it:

if (doc.security.allows('doc.download')) {
  showDownloadButton();
}

The engine checks every call too. A call the user may not make fails with Forbidden, so hiding a button is about honest UI, not safety.

Where permissions come from#

They’re in the user’s token, which your backend creates. The server checks them on every request. See Authentication.

To list what the user was given (for a “you can…” panel), read doc.security.scope.

Every permission#

PermissionWhat it allows
doc.openOpen the document and list its pages.
doc.renderRender pages as images.
doc.render.thumbnailRender thumbnails: small pictures of whole pages.
doc.text.selectWhere text is: selecting it and the text layout.
doc.text.searchSearch, with where each match is.
doc.text.copyThe text itself: reading it, copying it, search snippets.
doc.content.copyTaking part of a page as an image: the viewer’s snapshot tool.
doc.downloadDownload the file, attached files and stamp drawings, signed revisions; export annotations; extract pages.
doc.printPrint, in the viewer: pictures of the pages at 150 DPI.
doc.print.highPrint at full quality: 300 DPI.
doc.pages.assembleRotate, move, delete, insert and name pages.
doc.pages.modifyChange page content: flatten, redact (with the permissions below).
doc.annotate.readSee annotations.
doc.annotate.modifyCreate, change and delete any annotation.
doc.annotate.importImport annotations with their original authors and dates.
doc.forms.readSee form fields and their values.
doc.forms.fillFill in form fields.
doc.forms.modifyCreate, change and delete form fields.
doc.forms.importImport form fields with who created them, and when.
doc.forms.scriptWrite scripts, submit buttons and links into form fields.
doc.signSign the document.
doc.sign.certifySign as the certifying signature.
doc.metadata.modifyChange the title, author and other metadata.
doc.outline.modifyAdd, change, move and delete outline items (bookmarks).
doc.attachments.modifyAdd and remove attached files.
doc.redactApply redactions.
pdf.permissionsWhat the PDF file itself allows (its permission flags); see below.
*Everything.

Some permissions include others: doc.render includes doc.render.thumbnail, doc.annotate.modify includes doc.annotate.read, doc.forms.fill includes doc.forms.read, and doc.forms.modify includes doc.forms.fill and doc.forms.read. Each page of these docs says which permission its calls need.

Each kind of page picture needs its own permission and no other: doc.render for pictures on screen, doc.render.thumbnail for thumbnails, doc.print for printing. So a scope can allow thumbnails, or printing, without doc.render. What a picture draws besides the page needs the permission to read it, whatever the picture: annotations need doc.annotate.read, form fields doc.forms.read.

doc.annotate.import, doc.forms.import and doc.forms.script come only from a scope that names them (or *): no other permission, and not pdf.permissions, includes them. doc.forms.script is about writing code into the form, never running it. Scripts run in each viewer, for everyone who fills the form in, so putting one in is a separate decision from designing fields.

Plugins’ permissions#

A plugin on your server adds its own permissions, named after the plugin: protected-display.view, say. They travel in tokens like the ones above, and the plugin checks them; the engine only passes them on, so they show up in doc.security.scope. A plugin permission works only where its plugin is installed: the server refuses to issue a token or a grant that names one it doesn’t know, and * includes every installed plugin’s permissions. The first part of a permission says whose it is, so doc., docs., pdf., tokens., grants. and the other core prefixes are never a plugin’s.

Check one like the core’s:

if (doc.security.allows('protected-display.view')) {
  // …
}

Once the session’s /access has answered, that’s the server’s answer, which knows the installed plugins; before, it’s whether the token names the permission, or *. A plugin declares its permissions, so they’re typed like the core’s and a typo is an error:

declare module '@embedpdf/core' {
  interface PluginPermissions {
    'protected-display.view': true;
  }
}

Some plugin permissions are another way of doing what a core one does: protected-display.view shows the pages as protected video, where doc.render shows them as pictures. A token may hold both, and then the core one has its usual effect: a reader who may render the pages sees them the usual way, and the plugin stays out of it. To protect a reader’s pages, leave doc.render out.

Annotations#

In a document many people work on, “may edit annotations” usually means “may edit my annotations”. Permissions for annotations can say exactly that, with the action and whose annotations it covers:

const scope = [
  'doc.open',
  'doc.render',
  'doc.annotate.read',
  'annotations:create:self',
  'annotations:update:self',
  'annotations:delete:self',
];
PartValues
actioncreate, update, delete, set-group, or * for all four
whoseself (the user’s own), all, group=<id> (a group’s), createdBy=<user id>

A user’s own annotations are the ones with their user id (userId). An annotations:<action>:… permission replaces doc.annotate.modify for that action. ['doc.annotate.modify', 'annotations:update:self'] lets a user create and delete any annotation, but update only their own.

To check a single annotation before showing its edit controls:

doc.security.allowsAnnotation('create'); // may the user add annotations?
doc.security.allowsAnnotation('update', annotation); // may they change this one?
doc.security.allowsAnnotation('delete', annotation); // …delete this one?

A form field’s widget is an annotation in the PDF, but changing it is designing the form: moving, resizing, restyling or deleting a widget takes doc.forms.modify, and the annotations:… permissions never apply to it. allowsAnnotation('update', widget) answers the same way.

Form fields#

When several people fill in one form, each fills in and signs their own fields: the buyer the buyer’s, the seller the seller’s. A field belongs to a group (groupId, see Signers and groups), and permissions for fields name the action and the group:

// The buyer: fills in and signs the buyer's fields, and nothing else.
const scope = ['doc.open', 'doc.render', 'fields:fill:group=buyer', 'fields:sign:group=buyer'];
PartValues
actionfill, sign, set-group, or * for all three
whichall, or group=<id> (the fields of one group)

A fields:fill:… permission replaces doc.forms.fill for filling in, and fields:sign:… replaces doc.sign for signing, so ['doc.forms.fill', 'fields:fill:group=buyer'] fills in the buyer’s fields only. An unassigned field is then left to users without a fields:fill permission. Any fields: permission lets the user read the form: groups limit what they change, never what they see.

A field anyone fills in (assignment: { kind: 'anyone' }) is for every signer: any user who may fill in some field may fill it in, whatever group their permission names. Giving a field to anyone takes fields:set-group:all, since it reaches every group.

A field a script calculates, such as a total, is filled in together with the field it’s calculated from. Writing it is allowed when the same change also fills in one of the user’s own fields, so the result travels with its input.

Designing the form (doc.forms.modify) isn’t narrowed by any fields: permission. A signer’s token lists the permissions it needs and leaves out doc.forms.modify and pdf.permissions.

doc.security.allowsField('fill', field); // may the user fill this field in?
doc.security.allowsField('sign', field); // …sign it?
doc.security.allowsField('set-group', { groupId: 'buyer' }); // …put fields in this group?

The PDF’s own restrictions#

A PDF can restrict itself: a file protected with an owner password may forbid printing or copying. Add pdf.permissions to the scope to follow those restrictions. Without it, the engine ignores them and only your permissions count.

Put pdf.permissions in the token’s scope, next to the other permissions.

pdf.permissions gives what Acrobat allows on the same file. The names are the ones in Acrobat’s security summary for the file:

The file allowspdf.permissions gives
Printingdoc.print, and doc.print.high when it allows printing at full quality
Content copyingdoc.text.copy and doc.content.copy, and with them a search’s snippets
Changing the documentdoc.pages.modify, doc.pages.assemble, doc.redact, doc.metadata.modify, doc.attachments.modify, doc.outline.modify, doc.forms.fill and doc.sign
Commentingdoc.annotate.modify, doc.forms.fill and doc.sign
Changing the document and commentingall of those, and doc.forms.modify
Filling in form fieldsdoc.forms.fill and doc.sign
Document assemblydoc.pages.assemble and doc.outline.modify

It always gives doc.open, doc.render, doc.annotate.read, doc.forms.read, doc.text.select and doc.text.search too: the file’s restrictions are about changing it and taking its content out, never about seeing its annotations and fields, or finding and selecting its text, which Acrobat allows in a file that forbids copying. Signing a signature field counts as filling it in, so whatever allows filling in allows signing. Designing the form takes both changing the document and commenting, as in Acrobat, where a file that only allows commenting and filling in offers no form tools.

When the user unlocks the file with its owner password, the file’s restrictions no longer apply. See Opening documents.

What signatures forbid#

A signed document can forbid changes itself: a certification says which changes may follow. That binds every user, whatever their permissions. allows() answers no, and the call fails with ProtectedDocument rather than Forbidden:

// On a document certified at level 2: filling in the form, but no new comments.
doc.security.allows('doc.forms.fill'); // true
doc.security.allows('doc.annotate.modify'); // false

See Certify a document for what each level allows.

When a call is refused#

A refused call fails with an EngineError whose code is Forbidden. It names the permission that was missing:

import { EngineError, EngineErrorCode } from '@cloudpdf/engine';
 
try {
  await doc.download();
} catch (error) {
  if (EngineError.is(error, EngineErrorCode.Forbidden)) {
    console.log(`missing ${error.details.required}`); // "doc.download"
  }
}

Good to know#

Building interface from permissions#

The headless components turn permissions into one question per button, such as annotation.canEdit(ref) and selection.canCopy(), so your interface never offers something that will fail. See Permissions in the headless docs.

Groups#

An annotation or a form field can belong to a group (groupId): a team or a department for annotations, whoever fills them in for fields. An annotation a user creates goes in their own group (identity.groupId). A form field is in no group until its designer names one, because a field is usually made for someone else. annotations:set-group:group=<id> and fields:set-group:group=<id> let a user put annotations or fields in that group; annotations:update:group=<id> lets them edit that group’s annotations, and fields:fill:group=<id> fill in that group’s fields.

A user’s own group takes no set-group permission. An annotation’s group can be changed, never removed. A field’s can be removed (groupId: null, or assignment: { kind: 'unassigned' }), which takes doc.forms.modify alone. A field can be for anyone instead of one group (assignment: { kind: 'anyone' }): every user with a fields:fill permission, or doc.forms.fill, fills it in.

A group=<id> permission names the group it covers: the user doesn’t need to be in it. To give someone several groups, give them one permission per group.

Importing with original authors#

doc.annotate.import lets an import write annotations with the authors, dates and owners they had in the source document. Only give it to trusted jobs, such as syncing from your own database: it lets the caller create annotations in someone else’s name.

doc.forms.import does the same for form fields: an import keeps who created each field, instead of naming the importing user.

Scripts in forms#

doc.forms.script decides who may put code into a form: scripts that check or calculate a field, and buttons that submit the form or open a link. A script runs for everyone who fills the form in afterwards. In your viewer it can change what they type or hide fields; opened in another PDF app, a script or a submit button can send their answers anywhere. So a user who may design fields (doc.forms.modify) doesn’t get it with that. Buttons that only go to a page, reset the form, or show and hide fields need doc.forms.modify alone, and so does a field’s date or time format: the engine writes its two scripts itself, from a pattern it checked. An import without it leaves the scripts, submits and links out and copies the rest.

It doesn’t make a script’s result trustworthy: a client can skip scripts and send any value. A server that must trust a calculated value calculates it itself.