feat: opt-in JPEG-embed quality for image-to-PDF conversion

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-30 23:30:08 +02:00
co-authored by Claude Sonnet 5
parent 57d7dac2b5
commit fd99453e38
2 changed files with 39 additions and 4 deletions
+31
View File
@@ -2,6 +2,7 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import sharp from 'sharp';
import { registerImageToPdfConverter } from '../../src/converters/imageToPdf.js';
import { resolve, listTargetFormats } from '../../src/converters/registry.js';
import { detectInputMime } from '../../src/mime.js';
@@ -33,4 +34,34 @@ describe('image to PDF converter', () => {
const detected = await detectInputMime(outputPath);
expect(detected.mime).toBe('application/pdf');
});
it('embeds a JPEG instead of a PNG when a quality is given, shrinking the output for a noisy image', async () => {
const inputPath = path.join(tmpDir, 'noisy.png');
await sharp({
create: {
width: 256,
height: 256,
channels: 3,
noise: { type: 'gaussian', mean: 128, sigma: 40 },
},
})
.png()
.toFile(inputPath);
const defaultOutputPath = path.join(tmpDir, 'default.pdf');
const compressedOutputPath = path.join(tmpDir, 'compressed.pdf');
const entry = resolve('png', 'pdf');
await entry.convert(inputPath, defaultOutputPath);
await entry.convert(inputPath, compressedOutputPath, { quality: 20 });
const compressedDetected = await detectInputMime(compressedOutputPath);
expect(compressedDetected.mime).toBe('application/pdf');
const [defaultStat, compressedStat] = await Promise.all([
fs.stat(defaultOutputPath),
fs.stat(compressedOutputPath),
]);
expect(compressedStat.size).toBeLessThan(defaultStat.size);
});
});