farmcontrol-server/src/pdfUtils.js
Tom Butcher 55b4c1a42e
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good
Add PDF conversion utilities and update build scripts for Linux packaging
Introduce a new pdfUtils.js file for on-demand PDF conversion using pdf-to-img and sharp. Update package.json to include new build scripts for Linux packaging, enhancing the build process with public package handling. Add .npmrc for public hoisting patterns and create build-linux-packages.sh for streamlined package creation.
2026-07-26 21:47:21 +01:00

62 lines
1.6 KiB
JavaScript

/**
* PDF conversion utilities loaded on demand so headless startup
* does not require ESM-only pdf-to-img at module load time.
*/
export async function convertPDFToImage(pdfInput, options = {}) {
const [{ pdf }, sharp] = await Promise.all([
import('pdf-to-img'),
import('sharp')
])
try {
let scale = options.scale || 2
const pdfOptions = {
scale,
...options
}
const document = await pdf(pdfInput, pdfOptions)
const outputImages = []
if (
options.page_numbers &&
Array.isArray(options.page_numbers) &&
options.page_numbers.length > 0
) {
for (const pageNum of options.page_numbers) {
let image = await document.getPage(pageNum)
if (options.width || options.height) {
const resizeOptions = {}
if (options.width) resizeOptions.width = options.width
if (options.height) resizeOptions.height = options.height
image = await sharp.default(image).resize(resizeOptions).toBuffer()
}
outputImages.push(image)
}
} else {
for await (const image of document) {
let processedImage = image
if (options.width || options.height) {
const resizeOptions = {}
if (options.width) resizeOptions.width = options.width
if (options.height) resizeOptions.height = options.height
processedImage = await sharp.default(image)
.resize(resizeOptions)
.toBuffer()
}
outputImages.push(processedImage)
}
}
return outputImages
} catch (error) {
console.error('Error converting PDF to image:', error)
throw error
}
}