Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

fix(lecture): set fallback DPI for extracted images (#1470)

Corentin Thomasseta58b4f0e3496
diff --git a/.changeset/real-lilies-smash.md b/.changeset/real-lilies-smash.md
new file mode 100644
index 00000000..44c860f0
--- /dev/null
+++ b/.changeset/real-lilies-smash.md
@@ -0,0 +1,6 @@
+---
+'@papra/app': patch
+'@papra/lecture': patch
+---
+
+Silenced the content extraction warning about DPI.
diff --git a/packages/lecture/src/pdf/pdf.usecases.test.ts b/packages/lecture/src/pdf/pdf.usecases.test.ts
index ff544a6f..892e5ff0 100644
--- a/packages/lecture/src/pdf/pdf.usecases.test.ts
+++ b/packages/lecture/src/pdf/pdf.usecases.test.ts
@@ -1,6 +1,7 @@
+import sharp from 'sharp';
import { describe, expect, test } from 'vitest';
import { IMAGE_KIND } from './pdf.constants';
-import { getNormalizedImageData } from './pdf.usecases';
+import { getNormalizedImageData, pdfImageToBuffer } from './pdf.usecases';
describe('getNormalizedImageData', () => {
describe('for GRAYSCALE_1BPP images, unpacks packed bits into one byte per pixel so that sharp can process the image', () => {
@@ -84,3 +85,20 @@ describe('getNormalizedImageData', () => {
expect(getNormalizedImageData(image)).toBe(data);
});
});
+
+describe('pdfImageToBuffer', () => {
+ test('sets Tesseract fallback density metadata on the generated PNG', async () => {
+ const image = {
+ data: new Uint8Array([255, 255, 255]),
+ width: 1,
+ height: 1,
+ kind: IMAGE_KIND.RGB_24BPP,
+ };
+
+ const buffer = await pdfImageToBuffer(image);
+ const metadata = await sharp(buffer).metadata();
+
+ expect(metadata.density).toBe(70);
+ expect(metadata.resolutionUnit).toBe('inch');
+ });
+});
diff --git a/packages/lecture/src/pdf/pdf.usecases.ts b/packages/lecture/src/pdf/pdf.usecases.ts
index d652011e..e6906aaf 100644
--- a/packages/lecture/src/pdf/pdf.usecases.ts
+++ b/packages/lecture/src/pdf/pdf.usecases.ts
@@ -27,13 +27,19 @@ export function getNormalizedImageData(image: PdfRawImage): Uint8Array {
export async function pdfImageToBuffer(image: PdfRawImage): Promise<Buffer> {
const imageData = getNormalizedImageData(image);
- return sharp(imageData, {
- raw: {
- width: image.width,
- height: image.height,
- channels: IMAGE_KIND_CHANNELS[image.kind]!,
- },
- })
- .png()
- .toBuffer();
+ return (
+ sharp(imageData, {
+ raw: {
+ width: image.width,
+ height: image.height,
+ channels: IMAGE_KIND_CHANNELS[image.kind]!,
+ },
+ })
+ // PDF image objects have no standalone DPI. Use Tesseract's 70 DPI fallback explicitly so
+ // it does not emit an invalid-resolution warning when processing the generated PNG.
+ // It's just a meta, doesn't affect the actual pixel data
+ .withMetadata({ density: 70 })
+ .png()
+ .toBuffer()
+ );
}