mirror of
https://github.com/documenso/documenso.git
synced 2026-06-22 04:12:06 +10:00
7a94ee3b83
Use Gemini to handle detection of recipients and fields within documents. Opt in using organisation or team settings. Replaces #2128 since the branch was cursed and would include dependencies that weren't even in the lock file. https://github.com/user-attachments/assets/e6cbb58f-62b9-4079-a9ae-7af5c4f2e4ec
41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
import { createCanvas } from '@napi-rs/canvas';
|
|
import pMap from 'p-map';
|
|
import * as pdfjsLib from 'pdfjs-dist/legacy/build/pdf.mjs';
|
|
|
|
export type PdfToImagesOptions = {
|
|
scale?: number;
|
|
};
|
|
|
|
export const pdfToImages = async (pdfBytes: Uint8Array, options: PdfToImagesOptions = {}) => {
|
|
const { scale = 2 } = options;
|
|
|
|
const pdf = await pdfjsLib.getDocument({ data: pdfBytes }).promise;
|
|
|
|
return await pMap(
|
|
Array.from({ length: pdf.numPages }),
|
|
async (_, index) => {
|
|
const pageNumber = index + 1;
|
|
const page = await pdf.getPage(pageNumber);
|
|
|
|
const viewport = page.getViewport({ scale });
|
|
|
|
const canvas = createCanvas(viewport.width, viewport.height);
|
|
|
|
await page.render({
|
|
// @ts-expect-error napi-rs/canvas satifies the requirements
|
|
canvas,
|
|
viewport,
|
|
}).promise;
|
|
|
|
return {
|
|
pageNumber,
|
|
image: await canvas.encode('jpeg'),
|
|
width: Math.floor(viewport.width),
|
|
height: Math.floor(viewport.height),
|
|
mimeType: 'image/jpeg',
|
|
};
|
|
},
|
|
{ concurrency: 10 },
|
|
);
|
|
};
|