feat: add image to PDF converter
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
import fs from 'node:fs/promises';
|
||||||
|
import sharp from 'sharp';
|
||||||
|
import { PDFDocument } from 'pdf-lib';
|
||||||
|
import { register } from './registry.js';
|
||||||
|
|
||||||
|
const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif'];
|
||||||
|
|
||||||
|
async function convert(inputPath, outputPath) {
|
||||||
|
const pngBuffer = await sharp(inputPath).png().toBuffer();
|
||||||
|
const metadata = await sharp(pngBuffer).metadata();
|
||||||
|
|
||||||
|
const pdfDoc = await PDFDocument.create();
|
||||||
|
const page = pdfDoc.addPage([metadata.width, metadata.height]);
|
||||||
|
const embeddedImage = await pdfDoc.embedPng(pngBuffer);
|
||||||
|
|
||||||
|
page.drawImage(embeddedImage, {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: metadata.width,
|
||||||
|
height: metadata.height,
|
||||||
|
});
|
||||||
|
|
||||||
|
const pdfBytes = await pdfDoc.save();
|
||||||
|
await fs.writeFile(outputPath, pdfBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function registerImageToPdfConverter() {
|
||||||
|
for (const sourceFormat of IMAGE_FORMATS) {
|
||||||
|
register({ family: 'image', sourceFormat, targetFormat: 'pdf', convert });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
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 { registerImageToPdfConverter } from '../../src/converters/imageToPdf.js';
|
||||||
|
import { resolve, listTargetFormats } from '../../src/converters/registry.js';
|
||||||
|
import { detectInputMime } from '../../src/mime.js';
|
||||||
|
|
||||||
|
let tmpDir;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
registerImageToPdfConverter();
|
||||||
|
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'converter-image-to-pdf-'));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await fs.rm(tmpDir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('image to PDF converter', () => {
|
||||||
|
it('registers pdf as a target for every image format', () => {
|
||||||
|
expect(listTargetFormats('png')).toContain('pdf');
|
||||||
|
expect(listTargetFormats('jpg')).toContain('pdf');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('converts a PNG fixture into a valid PDF', async () => {
|
||||||
|
const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png');
|
||||||
|
const outputPath = path.join(tmpDir, 'output.pdf');
|
||||||
|
const entry = resolve('png', 'pdf');
|
||||||
|
|
||||||
|
await entry.convert(inputPath, outputPath);
|
||||||
|
|
||||||
|
const detected = await detectInputMime(outputPath);
|
||||||
|
expect(detected.mime).toBe('application/pdf');
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user