2024-08-22 18:20:54 +02:00
|
|
|
import { PdfPage } from '../types/drawing.ts'
|
2024-07-25 17:51:31 +03:00
|
|
|
import { PDFDocument } from 'pdf-lib'
|
|
|
|
import { Mark } from '../types/mark.ts'
|
|
|
|
|
2024-09-02 12:59:35 +02:00
|
|
|
import * as PDFJS from 'pdfjs-dist'
|
|
|
|
import PDFJSWorker from 'pdfjs-dist/build/pdf.worker.mjs?worker'
|
|
|
|
if (!PDFJS.GlobalWorkerOptions.workerPort) {
|
|
|
|
// Use workerPort and allow worker to be shared between all getDocument calls
|
|
|
|
const worker = new PDFJSWorker()
|
|
|
|
PDFJS.GlobalWorkerOptions.workerPort = worker
|
|
|
|
}
|
2024-07-16 12:36:18 +03:00
|
|
|
|
2024-08-02 11:39:00 +01:00
|
|
|
/**
|
|
|
|
* Defined font size used when generating a PDF. Currently it is difficult to fully
|
|
|
|
* correlate font size used at the time of filling in / drawing on the PDF
|
|
|
|
* because it is dynamically rendered, and the final size.
|
|
|
|
* This should be fixed going forward.
|
|
|
|
* Switching to PDF-Lib will most likely make this problem redundant.
|
|
|
|
*/
|
2024-08-23 13:39:49 +02:00
|
|
|
export const FONT_SIZE: number = 16
|
2024-08-02 11:39:00 +01:00
|
|
|
/**
|
|
|
|
* Current font type used when generating a PDF.
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const FONT_TYPE: string = 'Arial'
|
2024-07-16 12:36:18 +03:00
|
|
|
|
2024-07-25 17:51:31 +03:00
|
|
|
/**
|
2024-08-23 21:30:32 +02:00
|
|
|
* A utility that transforms a drawing coordinate number into a CSS-compatible pixel string
|
2024-07-25 17:51:31 +03:00
|
|
|
* @param coordinate
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const inPx = (coordinate: number): string => `${coordinate}px`
|
2024-07-17 11:25:02 +03:00
|
|
|
|
2024-07-25 17:51:31 +03:00
|
|
|
/**
|
|
|
|
* A utility that checks if a given file is of the pdf type
|
|
|
|
* @param file
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const isPdf = (file: File) => file.type.toLowerCase().includes('pdf')
|
2024-07-16 12:36:18 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the pdf file binaries
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const readPdf = (file: File): Promise<string | ArrayBuffer> => {
|
2024-07-16 12:36:18 +03:00
|
|
|
return new Promise((resolve, reject) => {
|
2024-08-13 12:48:52 +03:00
|
|
|
const reader = new FileReader()
|
2024-07-16 12:36:18 +03:00
|
|
|
|
2024-08-16 12:01:41 +02:00
|
|
|
reader.onload = (e) => {
|
|
|
|
const data = e.target?.result
|
|
|
|
// Make sure we only resolve for string or ArrayBuffer type
|
|
|
|
// They are accepted by PDFJS.getDocument function
|
|
|
|
if (data && typeof data !== 'undefined') {
|
|
|
|
resolve(data)
|
|
|
|
} else {
|
|
|
|
reject(new Error('File is null or undefined'))
|
|
|
|
}
|
2024-08-13 12:48:52 +03:00
|
|
|
}
|
2024-07-16 12:36:18 +03:00
|
|
|
|
|
|
|
reader.onerror = (err) => {
|
|
|
|
console.error('err', err)
|
|
|
|
reject(err)
|
2024-08-13 12:48:52 +03:00
|
|
|
}
|
2024-07-16 12:36:18 +03:00
|
|
|
|
2024-08-13 12:48:52 +03:00
|
|
|
reader.readAsDataURL(file)
|
2024-07-16 12:36:18 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-28 11:41:29 +02:00
|
|
|
export const getInnerContentWidth = () => {
|
|
|
|
// Fetch the first container element we find
|
|
|
|
const element = document.querySelector('#content-preview')
|
|
|
|
|
|
|
|
if (element) {
|
|
|
|
const style = getComputedStyle(element)
|
|
|
|
|
|
|
|
// Calculate width without padding
|
|
|
|
const widthWithoutPadding =
|
|
|
|
element.clientWidth - parseFloat(style.padding) * 2
|
|
|
|
|
|
|
|
return widthWithoutPadding
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default value
|
|
|
|
return 620
|
|
|
|
}
|
|
|
|
|
2024-07-16 12:36:18 +03:00
|
|
|
/**
|
|
|
|
* Converts pdf to the images
|
|
|
|
* @param data pdf file bytes
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const pdfToImages = async (
|
|
|
|
data: string | ArrayBuffer
|
|
|
|
): Promise<PdfPage[]> => {
|
2024-08-23 13:39:49 +02:00
|
|
|
const pages: PdfPage[] = []
|
2024-08-13 12:48:52 +03:00
|
|
|
const pdf = await PDFJS.getDocument(data).promise
|
|
|
|
const canvas = document.createElement('canvas')
|
2024-08-28 11:41:29 +02:00
|
|
|
const width = getInnerContentWidth()
|
2024-07-16 12:36:18 +03:00
|
|
|
|
|
|
|
for (let i = 0; i < pdf.numPages; i++) {
|
2024-08-13 12:48:52 +03:00
|
|
|
const page = await pdf.getPage(i + 1)
|
2024-08-23 13:39:49 +02:00
|
|
|
|
|
|
|
const originalViewport = page.getViewport({ scale: 1 })
|
2024-08-23 21:30:32 +02:00
|
|
|
const scale = width / originalViewport.width
|
|
|
|
const viewport = page.getViewport({ scale: scale })
|
2024-08-13 12:48:52 +03:00
|
|
|
const context = canvas.getContext('2d')
|
|
|
|
canvas.height = viewport.height
|
|
|
|
canvas.width = viewport.width
|
2024-08-23 13:39:49 +02:00
|
|
|
|
2024-08-13 12:48:52 +03:00
|
|
|
await page.render({ canvasContext: context!, viewport: viewport }).promise
|
2024-08-23 13:39:49 +02:00
|
|
|
pages.push({
|
|
|
|
image: canvas.toDataURL(),
|
2024-08-27 15:24:19 +02:00
|
|
|
width: originalViewport.width,
|
2024-08-23 13:39:49 +02:00
|
|
|
drawnFields: []
|
|
|
|
})
|
2024-07-16 12:36:18 +03:00
|
|
|
}
|
|
|
|
|
2024-08-23 13:39:49 +02:00
|
|
|
return pages
|
2024-07-16 12:36:18 +03:00
|
|
|
}
|
|
|
|
|
2024-07-25 17:51:31 +03:00
|
|
|
/**
|
|
|
|
* Takes in individual pdf file and an object with Marks grouped by Page number
|
|
|
|
* Returns an array of encoded images where each image is a representation
|
|
|
|
* of a PDF page with completed and signed marks from all users
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const addMarks = async (
|
2024-08-13 12:48:52 +03:00
|
|
|
file: File,
|
|
|
|
marksPerPage: { [key: string]: Mark[] }
|
|
|
|
) => {
|
|
|
|
const p = await readPdf(file)
|
|
|
|
const pdf = await PDFJS.getDocument(p).promise
|
|
|
|
const canvas = document.createElement('canvas')
|
2024-07-25 17:51:31 +03:00
|
|
|
|
2024-08-13 12:48:52 +03:00
|
|
|
const images: string[] = []
|
2024-07-25 17:51:31 +03:00
|
|
|
|
2024-08-13 12:48:52 +03:00
|
|
|
for (let i = 0; i < pdf.numPages; i++) {
|
|
|
|
const page = await pdf.getPage(i + 1)
|
2024-08-23 13:39:49 +02:00
|
|
|
const viewport = page.getViewport({ scale: 1 })
|
2024-08-13 12:48:52 +03:00
|
|
|
const context = canvas.getContext('2d')
|
|
|
|
canvas.height = viewport.height
|
|
|
|
canvas.width = viewport.width
|
2024-08-23 13:39:49 +02:00
|
|
|
if (context) {
|
|
|
|
await page.render({ canvasContext: context, viewport: viewport }).promise
|
2024-07-25 17:51:31 +03:00
|
|
|
|
2024-08-23 13:39:49 +02:00
|
|
|
if (marksPerPage && Object.hasOwn(marksPerPage, i)) {
|
2024-08-23 21:30:32 +02:00
|
|
|
marksPerPage[i]?.forEach((mark) => draw(mark, context))
|
2024-08-23 13:39:49 +02:00
|
|
|
}
|
2024-07-25 17:51:31 +03:00
|
|
|
|
2024-08-23 13:39:49 +02:00
|
|
|
images.push(canvas.toDataURL())
|
|
|
|
}
|
2024-07-25 17:51:31 +03:00
|
|
|
}
|
|
|
|
|
2024-08-23 13:39:49 +02:00
|
|
|
canvas.remove()
|
|
|
|
|
|
|
|
return images
|
2024-07-25 17:51:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility to scale mark in line with the PDF-to-PNG scale
|
|
|
|
*/
|
2024-08-23 13:39:49 +02:00
|
|
|
export const scaleMark = (mark: Mark, scale: number): Mark => {
|
2024-08-13 12:48:52 +03:00
|
|
|
const { location } = mark
|
2024-07-25 17:51:31 +03:00
|
|
|
return {
|
|
|
|
...mark,
|
|
|
|
location: {
|
|
|
|
...location,
|
2024-08-23 13:39:49 +02:00
|
|
|
width: location.width * scale,
|
|
|
|
height: location.height * scale,
|
|
|
|
left: location.left * scale,
|
|
|
|
top: location.top * scale
|
2024-07-25 17:51:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility to check if a Mark has value
|
|
|
|
* @param mark
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const hasValue = (mark: Mark): boolean => !!mark.value
|
2024-07-25 17:51:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a Mark on a Canvas representation of a PDF Page
|
|
|
|
* @param mark to be drawn
|
|
|
|
* @param ctx a Canvas representation of a specific PDF Page
|
|
|
|
*/
|
2024-08-23 21:30:32 +02:00
|
|
|
export const draw = (mark: Mark, ctx: CanvasRenderingContext2D) => {
|
|
|
|
const { location } = mark
|
|
|
|
ctx.font = FONT_SIZE + 'px ' + FONT_TYPE
|
2024-08-23 13:39:49 +02:00
|
|
|
ctx.fillStyle = 'black'
|
|
|
|
const textMetrics = ctx.measureText(mark.value!)
|
|
|
|
const textHeight =
|
|
|
|
textMetrics.actualBoundingBoxAscent + textMetrics.actualBoundingBoxDescent
|
2024-08-13 12:48:52 +03:00
|
|
|
const textX = location.left + (location.width - textMetrics.width) / 2
|
2024-08-23 13:39:49 +02:00
|
|
|
const textY = location.top + (location.height + textHeight) / 2
|
|
|
|
ctx.fillText(mark.value!, textX, textY)
|
2024-07-25 17:51:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes an array of encoded PDF pages and returns a blob that is a complete PDF file
|
|
|
|
* @param markedPdfPages
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const convertToPdfBlob = async (
|
|
|
|
markedPdfPages: string[]
|
|
|
|
): Promise<Blob> => {
|
2024-08-13 12:48:52 +03:00
|
|
|
const pdfDoc = await PDFDocument.create()
|
2024-07-25 17:51:31 +03:00
|
|
|
|
|
|
|
for (const page of markedPdfPages) {
|
|
|
|
const pngImage = await pdfDoc.embedPng(page)
|
|
|
|
const p = pdfDoc.addPage([pngImage.width, pngImage.height])
|
|
|
|
p.drawImage(pngImage, {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: pngImage.width,
|
|
|
|
height: pngImage.height
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const pdfBytes = await pdfDoc.save()
|
|
|
|
return new Blob([pdfBytes], { type: 'application/pdf' })
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param marks - an array of Marks
|
|
|
|
* @function hasValue removes any Mark without a property
|
|
|
|
* @function byPage groups remaining Marks by their page marks.location.page
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const groupMarksByFileNamePage = (marks: Mark[]) => {
|
2024-07-25 17:51:31 +03:00
|
|
|
return marks
|
|
|
|
.filter(hasValue)
|
2024-08-22 18:20:54 +02:00
|
|
|
.reduce<{ [fileName: string]: { [page: number]: Mark[] } }>(byPage, {})
|
2024-07-25 17:51:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reducer callback that transforms an array of marks into an object grouped by the page number
|
|
|
|
* Can be replaced by Object.groupBy https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
|
|
|
|
* when it is implemented in TypeScript
|
|
|
|
* Implementation is standard from the Array.prototype.reduce documentation
|
|
|
|
* @param obj - accumulator in the reducer callback
|
|
|
|
* @param mark - current value, i.e. Mark being examined
|
|
|
|
*/
|
2024-08-22 18:20:54 +02:00
|
|
|
export const byPage = (
|
2024-08-16 12:01:41 +02:00
|
|
|
obj: { [filename: string]: { [page: number]: Mark[] } },
|
|
|
|
mark: Mark
|
|
|
|
) => {
|
2024-08-22 18:20:54 +02:00
|
|
|
const fileName = mark.fileName
|
2024-08-16 12:01:41 +02:00
|
|
|
const pageNumber = mark.location.page
|
2024-08-22 18:20:54 +02:00
|
|
|
const pages = obj[fileName] ?? {}
|
2024-08-16 12:01:41 +02:00
|
|
|
const marks = pages[pageNumber] ?? []
|
|
|
|
return {
|
|
|
|
...obj,
|
2024-08-22 18:20:54 +02:00
|
|
|
[fileName]: {
|
2024-08-16 12:01:41 +02:00
|
|
|
...pages,
|
|
|
|
[pageNumber]: [...marks, mark]
|
|
|
|
}
|
|
|
|
}
|
2024-07-25 17:51:31 +03:00
|
|
|
}
|