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:
parent
6c0520c526
commit
c0415f9d87
File diff suppressed because it is too large
Load Diff
|
@ -1,49 +1,63 @@
|
||||||
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 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) {
|
export async function processPdf(file) {
|
||||||
var typedarray = new Uint8Array(file)
|
// Array to store ImageData information
|
||||||
|
const typedArray = new Uint8Array(file)
|
||||||
|
|
||||||
const canvas = document.getElementById('canvas')
|
// PDF scale, increase to read smaller QR Codes
|
||||||
const ctx = canvas.getContext('2d')
|
const pdfScale = 2;
|
||||||
|
|
||||||
var loadingTask = PDFJS.getDocument(typedarray)
|
// Get the canvas and context to render PDF
|
||||||
await loadingTask.promise.then(async function (pdfDocument) {
|
const canvas = document.getElementById('canvas')
|
||||||
const pdfPage = await pdfDocument.getPage(1)
|
const ctx = canvas.getContext('2d')
|
||||||
const viewport = pdfPage.getViewport({ scale: 1 })
|
|
||||||
canvas.width = viewport.width
|
|
||||||
canvas.height = viewport.height
|
|
||||||
|
|
||||||
const renderTask = pdfPage.render({
|
let loadingTask = PdfJS.getDocument(typedArray)
|
||||||
canvasContext: ctx,
|
|
||||||
viewport,
|
await loadingTask.promise.then(async function (pdfDocument) {
|
||||||
|
// 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,
|
||||||
|
})
|
||||||
|
|
||||||
|
return await renderTask.promise
|
||||||
})
|
})
|
||||||
|
|
||||||
return await renderTask.promise
|
|
||||||
})
|
|
||||||
|
|
||||||
var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
|
// Return PDF Image Data
|
||||||
|
return ctx.getImageData(0, 0, canvas.width, canvas.height)
|
||||||
return imageData
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Processes a JPEG File and returns it as ImageData
|
||||||
export async function processJpeg(file) {
|
export async function processJpeg(file) {
|
||||||
return jpeg.decode(file)
|
return jpeg.decode(file)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Processes a PNG File and returns it as ImageData
|
||||||
export async function processPng(file) {
|
export async function processPng(file) {
|
||||||
return new Promise(async (resolve, reject) => {
|
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) {
|
|
||||||
reject()
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(data)
|
png.parse(file, (error, data) => {
|
||||||
|
if (error) {
|
||||||
|
reject()
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(data)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue