Pagecraft

protect

Protect PDF

Encrypt a document with a password, on this device.

Reads
a PDF — application/pdf
Writes
one file — PDFs
Tool page
/protect-pdf

Example

Encrypt a document before emailing it, and say what a reader may do with it.

Given

  • payroll.pdf — an ordinary PDF of 2 pages

From code

            import { loadDocument, runOperation } from '@pagecraft/core';

const payroll = await loadDocument({ bytes, name: 'payroll.pdf' });

const [output] = await runOperation('protect', [payroll], {
  userPassword: 'correct horse battery staple',
  encryption: 'aes-256',
  allow: [
    'print',
    'accessibility',
  ],
});
          

From a shell

            pagecraft protect payroll.pdf --user-password 'correct horse battery staple' --encryption aes-256 --allow print,accessibility
          

What comes back

One PDF.

This example is run by the test suite. If it stopped being true, the build would fail.

Options

userPassword--user-password

string

Password to open

Asked for before the document will open at all. Leave empty to let it open freely.

A secret: never shown as it is typed, never remembered, and never written into a URL, a log or a shell history.

ownerPassword--owner-password

string

Password to change permissions

Asked for before the permissions below can be changed. Leave empty to use the same password.

A secret: never shown as it is typed, never remembered, and never written into a URL, a log or a shell history.

encryption--encryption

string, one of

Encryption

AES-256 unless you need a file a reader from before 2009 can open.

Default: 'aes-256'

  • aes-256 — The strongest, and the default.
  • aes-128 — For readers older than Acrobat 9.
  • rc4-128 — Weak. Only for a reader that understands nothing newer.

allow--allow

string[], any of

Allow

What a reader may do once the document is open. Enforcement is the reader’s choice, not a guarantee.

Default: ['print', 'high-quality-print', 'copy', 'modify', 'annotate', 'fill-forms', 'accessibility', 'assemble']

  • print — Printing
  • high-quality-print — High-quality printing
  • copy — Copying text and images
  • modify — Changing the content
  • annotate — Adding comments
  • fill-forms — Filling in forms
  • accessibility — Reading with assistive software
  • assemble — Inserting, rotating and deleting pages

What it cannot do

  • Permissions are honoured by the reader, not enforced by the file: a reader that ignores them can still print or copy. Only the open password actually withholds the content.
  • Reading with assistive software cannot be forbidden under AES-256. PDF 2.0 withdrew that permission and readers ignore it, so it is always allowed rather than promised and not kept.
  • Pagecraft cannot recover a password. Encrypt a document you can still open.