pdf convert wasm only

This commit is contained in:
seb
2026-09-20 04:09:22 +02:00
parent 9d88823167
commit 68372055fa
4 changed files with 20 additions and 39 deletions

View File

@@ -1,48 +1,19 @@
import sharp from 'sharp';
import { spawn } from 'node:child_process';
import { pdfToPng } from 'pdfconvert-wasm';
const THUMB_MAX = 200;
/**
* Renders page 1 of a PDF into a PNG buffer using pdftocairo.
* Renders page 1 of a PDF into a PNG buffer using pdfconvert-wasm.
* @param {Buffer} pdfBuffer
* @returns {Promise<Buffer>}
*/
function renderPdfPageToPng(pdfBuffer) {
return new Promise((resolve, reject) => {
const proc = spawn('pdftocairo', ['-png', '-singlefile', '-', '-'], {
stdio: ['pipe', 'pipe', 'pipe'],
});
const chunks = [];
const errChunks = [];
proc.stdout.on('data', (chunk) => chunks.push(chunk));
proc.stderr.on('data', (chunk) => errChunks.push(chunk));
proc.stdin.on('error', (err) => {
console.warn('[pdftocairo] stdin stream error:', err.message);
});
proc.on('error', (err) => {
reject(new Error(`Failed to start pdftocairo: ${err.message}`));
});
proc.on('close', (code) => {
if (code !== 0) {
const stderr = Buffer.concat(errChunks).toString('utf8').trim();
reject(new Error(`pdftocairo exited with code ${code}${stderr ? `: ${stderr}` : ''}`));
} else {
resolve(Buffer.concat(chunks));
}
});
try {
proc.stdin.end(pdfBuffer);
} catch (writeErr) {
console.warn('[pdftocairo] Failed writing to stdin:', writeErr.message);
}
});
async function renderPdfPageToPng(pdfBuffer) {
const images = await pdfToPng(pdfBuffer, { dpi: 150, singleFile: true });
if (!images || images.length === 0 || !images[0].data) {
throw new Error('pdfconvert-wasm produced no image output');
}
return Buffer.from(images[0].data);
}
/**
@@ -51,9 +22,10 @@ function renderPdfPageToPng(pdfBuffer) {
*
* @param {Buffer} content
* @param {string} mimetype
* @param {Object} [options]
* @returns {Promise<{thumbavif: Buffer|null, thumbx: number|null, thumby: number|null}>}
*/
export default async function generateDocumentThumb(content, mimetype) {
export default async function generateDocumentThumb(content, mimetype, options = {}) {
if (!Buffer.isBuffer(content) || content.length === 0) {
return { thumbavif: null, thumbx: null, thumby: null };
}