Refactor PDF conversion utilities for dynamic imports and option handling
All checks were successful
farmcontrol/farmcontrol-server/pipeline/head This commit looks good

- Replace static imports with dynamic imports using a helper function to improve compatibility with pkg snapshots.
- Simplify options handling by destructuring parameters, enhancing readability and maintainability.
- Update image processing logic to utilize destructured width and height options for resizing.
This commit is contained in:
Tom Butcher 2026-07-28 02:18:27 +01:00
parent 4677e17c7f
commit 5fe8d76310

View File

@ -2,35 +2,47 @@
* PDF conversion utilities loaded on demand so headless startup * PDF conversion utilities loaded on demand so headless startup
* does not require ESM-only pdf-to-img at module load time. * does not require ESM-only pdf-to-img at module load time.
*/ */
import { createRequire } from 'node:module'
import { pathToFileURL } from 'node:url'
const require = createRequire(import.meta.url)
// Bare dynamic import() fails in pkg snapshots with "Invalid host defined options".
async function importPackage(name) {
const packagePath = require.resolve(name)
return import(pathToFileURL(packagePath).href)
}
export async function convertPDFToImage(pdfInput, options = {}) { export async function convertPDFToImage(pdfInput, options = {}) {
const [{ pdf }, sharp] = await Promise.all([ const [{ pdf }, sharp] = await Promise.all([
import('pdf-to-img'), importPackage('pdf-to-img'),
import('sharp') importPackage('sharp')
]) ])
try { try {
let scale = options.scale || 2 const { width, height, page_numbers, scale = 2, ...pdfOnlyOptions } =
options
const pdfOptions = { const pdfOptions = {
scale, scale,
...options ...pdfOnlyOptions
} }
const document = await pdf(pdfInput, pdfOptions) const document = await pdf(pdfInput, pdfOptions)
const outputImages = [] const outputImages = []
if ( if (
options.page_numbers && page_numbers &&
Array.isArray(options.page_numbers) && Array.isArray(page_numbers) &&
options.page_numbers.length > 0 page_numbers.length > 0
) { ) {
for (const pageNum of options.page_numbers) { for (const pageNum of page_numbers) {
let image = await document.getPage(pageNum) let image = await document.getPage(pageNum)
if (options.width || options.height) { if (width || height) {
const resizeOptions = {} const resizeOptions = {}
if (options.width) resizeOptions.width = options.width if (width) resizeOptions.width = width
if (options.height) resizeOptions.height = options.height if (height) resizeOptions.height = height
image = await sharp.default(image).resize(resizeOptions).toBuffer() image = await sharp.default(image).resize(resizeOptions).toBuffer()
} }
@ -40,10 +52,10 @@ export async function convertPDFToImage(pdfInput, options = {}) {
for await (const image of document) { for await (const image of document) {
let processedImage = image let processedImage = image
if (options.width || options.height) { if (width || height) {
const resizeOptions = {} const resizeOptions = {}
if (options.width) resizeOptions.width = options.width if (width) resizeOptions.width = width
if (options.height) resizeOptions.height = options.height if (height) resizeOptions.height = height
processedImage = await sharp.default(image) processedImage = await sharp.default(image)
.resize(resizeOptions) .resize(resizeOptions)
.toBuffer() .toBuffer()