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:
@@ -5,13 +5,17 @@ import { register } from './registry.js';
|
|||||||
|
|
||||||
const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif'];
|
const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif'];
|
||||||
|
|
||||||
async function convert(inputPath, outputPath) {
|
async function convert(inputPath, outputPath, { quality } = {}) {
|
||||||
const pngBuffer = await sharp(inputPath).png().toBuffer();
|
const imageBuffer =
|
||||||
const metadata = await sharp(pngBuffer).metadata();
|
quality == null
|
||||||
|
? await sharp(inputPath).png().toBuffer()
|
||||||
|
: await sharp(inputPath).jpeg({ quality }).toBuffer();
|
||||||
|
const metadata = await sharp(imageBuffer).metadata();
|
||||||
|
|
||||||
const pdfDoc = await PDFDocument.create();
|
const pdfDoc = await PDFDocument.create();
|
||||||
const page = pdfDoc.addPage([metadata.width, metadata.height]);
|
const page = pdfDoc.addPage([metadata.width, metadata.height]);
|
||||||
const embeddedImage = await pdfDoc.embedPng(pngBuffer);
|
const embeddedImage =
|
||||||
|
quality == null ? await pdfDoc.embedPng(imageBuffer) : await pdfDoc.embedJpg(imageBuffer);
|
||||||
|
|
||||||
page.drawImage(embeddedImage, {
|
page.drawImage(embeddedImage, {
|
||||||
x: 0,
|
x: 0,
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { describe, it, expect, beforeAll, afterAll } from 'vitest';
|
|||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
|
import sharp from 'sharp';
|
||||||
import { registerImageToPdfConverter } from '../../src/converters/imageToPdf.js';
|
import { registerImageToPdfConverter } from '../../src/converters/imageToPdf.js';
|
||||||
import { resolve, listTargetFormats } from '../../src/converters/registry.js';
|
import { resolve, listTargetFormats } from '../../src/converters/registry.js';
|
||||||
import { detectInputMime } from '../../src/mime.js';
|
import { detectInputMime } from '../../src/mime.js';
|
||||||
@@ -33,4 +34,34 @@ describe('image to PDF converter', () => {
|
|||||||
const detected = await detectInputMime(outputPath);
|
const detected = await detectInputMime(outputPath);
|
||||||
expect(detected.mime).toBe('application/pdf');
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user