Hello I'm currently developing a function to extract an image of the first page of a pdf. I get the source pdf from s3 and upload the result to s3 as well.
I added ghostscript and graphicsmagick layers to the lambda, this is a snippet of my sst config
nodejs: {
esbuild: {
external: ['graphicsmagick', 'ghostscript4js', 'ghostscript-node'],
},
},
layers: [
new aws_lambda.LayerVersion(stack, 'GraphicsMagickLayer', {
code: aws_lambda.Code.fromAsset('./packages/functions/layers/gm-layer-1.3.43.zip'),
}),
new aws_lambda.LayerVersion(stack, 'GhostScript', {
code: aws_lambda.Code.fromAsset('./packages/functions/layers/ghostscript.zip'),
}),
],
And this is the lambda code
import { GetObjectCommand, PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
import { APIGatewayProxyEventV2 } from 'aws-lambda';
import { fromBuffer } from 'pdf2pic';
import { Config } from 'sst/node/config';
import { IngestEventProps } from './consumer.js';
const client = new S3Client({ region: 'eu-central-1' });
const processPdf = async (userId: string, fileName: string, pdfBuff: Buffer): Promise<number> => {
console.log('****** got input buff len: ', Buffer.byteLength(pdfBuff));
const convert = fromBuffer(pdfBuff, {
density: 100,
preserveAspectRatio: true,
format: 'png',
width: 595,
height: 842,
});
const pageToConvertAsImage = 1;
const outBuffer = await convert(pageToConvertAsImage, { responseType: 'buffer' });
if (outBuffer.buffer) {
console.log('****** outBuffer **** ', outBuffer);
console.log('****** out buff **** ', Buffer.byteLength(outBuffer.buffer));
} else {
console.error('No buffer generated by pdf2pic');
return 0;
}
console.log(Config.BUCKET_URL);
console.log(Config.BUCKET_BASE_PATH + `${userId}/${fileName}.png`);
const command = new PutObjectCommand({
Bucket: Config.BUCKET_URL,
Key: Config.BUCKET_BASE_PATH + `${userId}/${fileName}.png`,
Body: outBuffer.buffer,
ContentType: 'image/png',
});
await client.send(command);
console.log('after put command');
if (outBuffer.buffer) {
return Buffer.byteLength(outBuffer.buffer);
} else {
return 0;
}
};
export async function handler(event: APIGatewayProxyEventV2) {
console.log('got event');
console.log(event);
if (!event.body) {
console.error('[pdfthumb] No body');
return;
}
const parsedEvent = JSON.parse(event.body) as IngestEventProps;
const command = new GetObjectCommand({
Bucket: Config.BUCKET_URL,
Key: Config.BUCKET_BASE_PATH + `${parsedEvent.userId}/` + parsedEvent.fileName,
});
let outBuff = 0;
try {
const response = await client.send(command);
if (!response.Body) {
console.error('[pdfthumb] getobj s3 - No body');
return;
}
const byteArr = await response.Body.transformToByteArray();
const buffer = Buffer.from(byteArr);
outBuff = await processPdf(parsedEvent.userId, parsedEvent.fileName, buffer);
} catch (error) {
console.error('[pdfthumb] Unhandled error', error);
}
return { buff: outBuff };
}
As you can see from my code I logged both the input and output buffers. The input buffer has the correct value (not 0) but the output buffer length is always 0, what other checks can I do?
Thanks!!
Hello I'm currently developing a function to extract an image of the first page of a pdf. I get the source pdf from s3 and upload the result to s3 as well.
I added ghostscript and graphicsmagick layers to the lambda, this is a snippet of my sst config
And this is the lambda code
As you can see from my code I logged both the input and output buffers. The input buffer has the correct value (not 0) but the output buffer length is always 0, what other checks can I do?
Thanks!!