PDF changes

- Scale pdf by 2, in order to read smaller QR Codes
- Read last page of PDF (2nd dose often on 2nd page)
This commit is contained in:
Hauke Tönjes 2021-06-29 13:30:39 +02:00
parent 6c0520c526
commit c0415f9d87
No known key found for this signature in database
GPG Key ID: 0BF2BC96C9FAAE9E
2 changed files with 55 additions and 6575 deletions

6552
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,22 +1,35 @@
const PDFJS = require('pdfjs-dist')
PDFJS.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${PDFJS.version}/pdf.worker.js`
import jpeg from 'jpeg-js'
import { PNG } from 'pngjs'
import {PNG} from 'pngjs'
import * as PdfJS from 'pdfjs-dist'
PdfJS.GlobalWorkerOptions.workerSrc = `https://cdnjs.cloudflare.com/ajax/libs/pdf.js/${PdfJS.version}/pdf.worker.js`
// Processes a pdf file and returns it as ImageData
export async function processPdf(file) {
var typedarray = new Uint8Array(file)
// Array to store ImageData information
const typedArray = new Uint8Array(file)
// PDF scale, increase to read smaller QR Codes
const pdfScale = 2;
// Get the canvas and context to render PDF
const canvas = document.getElementById('canvas')
const ctx = canvas.getContext('2d')
var loadingTask = PDFJS.getDocument(typedarray)
let loadingTask = PdfJS.getDocument(typedArray)
await loadingTask.promise.then(async function (pdfDocument) {
const pdfPage = await pdfDocument.getPage(1)
const viewport = pdfPage.getViewport({ scale: 1 })
// Load last PDF page
const pageNumber = pdfDocument.numPages;
const pdfPage = await pdfDocument.getPage(pageNumber)
const viewport = pdfPage.getViewport({scale: pdfScale})
// Set correct canvas width / height
canvas.width = viewport.width
canvas.height = viewport.height
// render PDF
const renderTask = pdfPage.render({
canvasContext: ctx,
viewport,
@ -25,21 +38,22 @@ export async function processPdf(file) {
return await renderTask.promise
})
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
return imageData
// Return PDF Image Data
return ctx.getImageData(0, 0, canvas.width, canvas.height)
}
// Processes a JPEG File and returns it as ImageData
export async function processJpeg(file) {
return jpeg.decode(file)
}
// Processes a PNG File and returns it as ImageData
export async function processPng(file) {
return new Promise(async (resolve, reject) => {
let png = new PNG({ filterType: 4 })
let png = new PNG({filterType: 4})
png.parse(file, (error, data) => {
if(error) {
if (error) {
reject()
}