mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
pdfjs-dist went from 5 to 6 and PDFDocumentProxy lost destroy(). In 5 it was one line, `return this.loadingTask.destroy()`, so the task is what there is to destroy. Three calls were left: - pdfToImages called it after rendering. The call threw before its .catch could attach, so every render ended in "pdf.destroy is not a function" and the AI features that read a document got nothing. The task it also destroyed is the same task, so that call is the one kept. - The PDF viewer called it when a new document replaced an old one, inside the load's try, so switching documents showed the load error; and in the effect's cleanup. Both now destroy the loading task, as 5 did for them. pdf-to-images.test.ts renders a two-page PDF and checks each page comes back as a letter-size JPEG in order; against the old code it fails with the TypeError. Co-authored-by: Hanzo Dev <[email protected]>
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
/**
|
|
* Rendering a PDF's pages to images, for the AI features that read a document.
|
|
*
|
|
* Run: npx tsx --test packages/lib/__tests__/pdf-to-images.test.ts
|
|
*/
|
|
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
|
|
import { PDF, StandardFonts } from '@libpdf/core';
|
|
|
|
import { pdfToImages } from '../server-only/ai/pdf-to-images';
|
|
|
|
const pdfOf = async (pages: string[]) => {
|
|
const pdf = PDF.create();
|
|
|
|
for (const text of pages) {
|
|
const page = pdf.addPage({ size: 'letter' });
|
|
|
|
page.drawText(text, { x: 72, y: 700, font: StandardFonts.Helvetica, size: 12 });
|
|
}
|
|
|
|
return new Uint8Array(await pdf.save());
|
|
};
|
|
|
|
test('renders every page to a JPEG, in order', async () => {
|
|
const images = await pdfToImages(await pdfOf(['one', 'two']), { scale: 1 });
|
|
|
|
assert.deepEqual(
|
|
images.map(({ pageNumber }) => pageNumber),
|
|
[1, 2],
|
|
);
|
|
|
|
for (const { image, mimeType, width, height } of images) {
|
|
assert.equal(mimeType, 'image/jpeg');
|
|
// Every JPEG opens with the start-of-image marker.
|
|
assert.deepEqual([image[0], image[1]], [0xff, 0xd8]);
|
|
// US letter at scale 1: 612 by 792 points.
|
|
assert.deepEqual([width, height], [612, 792]);
|
|
}
|
|
});
|