Skip to content

Locked files

A PDF can need a password to open. Inkbar asks for it. When a locked file opens, a “Password required” dialog appears, and the reader types the password. There is nothing to set up. Try it in the sandbox, where the password is inkbar.

The dialog has a labelled password field, Open and Cancel.

  • A wrong password keeps the dialog open and says “Incorrect password. Try again.”
  • Open is disabled while the field is empty and while a try is running.
  • Cancel closes the dialog and leaves a panel that says the document is password protected, with an “Enter password” button. The button opens the dialog again.
  • Opening another file closes the dialog and starts fresh.

A password you pass to the root still applies first. If it is wrong, the dialog opens with the incorrect message already showing.

While the file is locked, the status is { state: 'locked', incorrect: boolean }. incorrect is true after a wrong password. The root and viewport carry data-state="locked".

The error event, or onError, still fires with a PasswordRequired error for each failed open, prompt or not.

Inkbar uses the typed password for that one load and for no other file. It does not reach the status, the error, an event or storage. With engine, the open document keeps the password until it closes, because a save opens the file again in the engine.

Pass passwordPrompt as false to handle locked files yourself. A locked file is then { state: 'error', error }, and error.code is PasswordRequired.

<script setup lang="ts">
import { InkRoot, InkViewport } from '@inkbar/vue'
import { assets } from 'virtual:inkbar-assets'
defineProps<{ file: File }>()
</script>
<template>
<InkRoot :src="file" :assets="assets" :password-prompt="false">
<InkViewport />
</InkRoot>
</template>

The panel behind the dialog is the locked part of InkViewport. Fill it to show your own. It receives { incorrect, promptPassword }, and promptPassword opens the dialog again.

<InkViewport>
<template #locked="{ incorrect, promptPassword }">
<p v-if="incorrect">That password did not work.</p>
<button @click="promptPassword">Unlock this file</button>
</template>
</InkViewport>

To ask for the password your way, turn the prompt off and place the dialog yourself. InkPasswordDialog in each framework package and createPasswordDialog in @inkbar/core/dom are the same dialog the built-in flow uses, with the same markup and theme. It holds no password. Open hands the typed value to your code.

The flow has four steps. A failed open reports PasswordRequired, and error.details.incorrect is true after a wrong password. Show the dialog with that value. On submit, retry with the typed password and mark the dialog pending. Close the dialog when the document is ready.

InkRoot retries when its password prop changes. InkPasswordDialog takes v-model:open, incorrect and pending, and emits submit with the password.

<script setup lang="ts">
import type { InkError } from '@inkbar/core'
import { InkPasswordDialog, InkRoot, InkViewport } from '@inkbar/vue'
import { ref } from 'vue'
import { assets } from 'virtual:inkbar-assets'
defineProps<{ file: File }>()
const password = ref<string>()
const open = ref(false)
const incorrect = ref(false)
const pending = ref(false)
function onError(error: InkError) {
if (error.code !== 'PasswordRequired') return
incorrect.value = error.details.incorrect === true
pending.value = false
open.value = true
}
function onSubmit(typed: string) {
// The same text as the try that just failed would not reload.
if (typed === password.value) return
pending.value = true
password.value = typed
}
</script>
<template>
<InkRoot
:src="file"
:assets="assets"
:password="password"
:password-prompt="false"
@error="onError"
@ready="open = false"
>
<InkViewport />
<InkPasswordDialog
v-model:open="open"
:incorrect="incorrect"
:pending="pending"
@submit="onSubmit"
/>
</InkRoot>
</template>