const PDFJS = require('pdfjs-dist') PDFJS.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${PDFJS.version}/pdf.worker.js` import jsQR from "jsqr"; import { decodeData } from "../src/decode" import Card from "../components/Card" import Alert from "../components/Alert" export default Form function Form() { const error = function(heading, message) { const alert = document.getElementById('alert') alert.setAttribute('style', null) document.getElementById('heading').innerHTML = heading document.getElementById('message').innerHTML = message } const processPdf = async function() { if (!document.getElementById('privacy').checked) { document.getElementById('download').disabled = true return } document.getElementById('spin').style.display = 'block' const file = document.getElementById('pdf').files[0] var fileReader = new FileReader() fileReader.onload = async function() { var typedarray = new Uint8Array(this.result) const canvas = document.getElementById('canvas') const ctx = canvas.getContext('2d') var loadingTask = PDFJS.getDocument(typedarray) await loadingTask.promise.then(async function (pdfDocument) { const pdfPage = await pdfDocument.getPage(1) const viewport = pdfPage.getViewport({ scale: 1 }) canvas.width = viewport.width canvas.height = viewport.height const renderTask = pdfPage.render({ canvasContext: ctx, viewport, }) return await renderTask.promise }) var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height) var code = jsQR(imageData.data, imageData.width, imageData.height, { inversionAttempts: 'dontInvert', }) if (code) { const rawData = code.data let decoded try { decoded = decodeData(rawData) } catch (error) { error('Invalid QR code found', 'Make sure that you picked the correct PDF') } const result = JSON.stringify({ decoded: decoded, raw: rawData }) document.getElementById('payload').setAttribute('value', result) document.getElementById('download').disabled = false } else { error('No QR code found', 'Try scanning the PDF again') } document.getElementById('spin').style.display = 'none' } fileReader.readAsArrayBuffer(file) } return (