Attach and scan
Most apps that take documents ask the user for a photo of one, such as a receipt for an expense or a signed form. This example lets the user take the photo or pick a file, edit it in PaperPic, and send the result to your server.
upload is the function from Upload to a server, which sends the image and fails when the server does not accept it.
html
<label>
Photo of the document
<input id="photo" type="file" accept="image/*" capture="environment" />
</label>
<div id="viewer" style="height: 480px"></div>
<p id="status" role="status"></p>ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'
const input = document.querySelector<HTMLInputElement>('#photo')!
const status = document.querySelector('#status')!
const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
alt: 'The document you attached',
maxOutputSize: 3000,
})
input.addEventListener('change', () => {
const file = input.files?.[0]
input.value = ''
if (file) {
status.textContent = ''
pic.setOptions({ src: file })
}
})
// The edit starts as soon as the photo is on display
pic.on('load', () => pic.edit())
pic.on('error', ({ phase }) => {
if (phase === 'load') {
status.textContent = 'This file is not an image the browser can open'
}
})
pic.on('apply', async ({ blob, type }) => {
status.textContent = 'Sending…'
try {
await upload(blob, type)
status.textContent = 'Sent'
}
catch {
status.textContent = 'The document could not be sent'
}
})Notes
capture="environment"opens the back camera on phones, and computers ignore it. Without it, phones offer the camera and the photo library, which suits documents the user already has.- Emptying the input after each choice lets the user pick the same photo again, such as to start over after Apply.
- The viewer opens in the edit mode once the photo loads, so the user goes straight to Straighten or Clean up. Leave out the
loadlistener to show the photo first. phase: 'load'tells a file the browser cannot open, such as a HEIC photo outside Safari, from a failed edit. Loading images lists the formats.- The photo stays in the browser until Apply calls
upload. PaperPic sends nothing on its own. maxOutputSizekeeps the upload small. Phone photos have 12 megapixels or more, and documents stay readable at 3000 pixels on the longest side.