I'm trying to have a lambda that will convert tiff files into PDF files to later upload them into S3. For that I'm using the tiff2pdf library.
I copied the library executable inside of the lambda. But I'm encountering errors. I actually tried different approachs. I'll post here the latest one.
Basically, I followed these steps here:
- I downloaded the source code of the
libtiff
library and compile it inside of an ec2 instance, using Amazon Linux. - Later on my lambda code base, I created a
bin
folder where I put the executable from the compilation of step 1. - I created a class to convert the pdf, that's suppose to execute the command
tiff2pdf -o file.pdf file.tiff
. - I uploaded my code base on the lambda created using Nodejs v18.
This is the class mentioned on point 3:
import { exec } from 'child_process';
import { promisify } from 'util';
import { join } from 'path';
import { readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
export default class TiffToPdfConverter {
static async convert(inputPath, outputPath) {
try {
// Load the bundled tiff2pdf binary as an asset
const tiff2pdfBinary = readFileSync(join(path.resolve(path.dirname(''), 'bin', 'tiff2pdf')));
// Write the binary to a temporary file in the /tmp directory
const tiff2pdfPath = '/tmp/tiff2pdf';
writeFileSync(tiff2pdfPath, tiff2pdfBinary, { mode: 0o755 });
// Execute the tiff2pdf conversion command using the provided binary path
const command = `${tiff2pdfPath} -o ${outputPath} ${inputPath}`;
console.log('command', command);
await promisify(exec)(command);
} catch (error) {
console.error('Error converting TIFF to PDF:', error);
throw error;
}
}
}
I had to move the library inside of the /tmp
library, because before I was having errors saying read only access. But even thought I keep having the following error:
/tmp/tiff2pdf: line 202: cd: /home/ec2-user/libtiff/tiff-4.5.0/tools: No such file or directory
/tmp/tiff2pdf: line 202: gcc: command not found
I noticed that inside of the executable there is a reference to that address. I just don't know how can I replace it. This is the reference inside of tiff2pdf executable:
relink_command="(cd /home/ec2-user/libtiff/tiff-4.5.0/tools; { test -z \"\${LIBRARY_PATH+set}\" || unset LIBRARY_PATH || { LIBRARY_PATH=; export LIBRARY_PATH; }; }; { test -z \"\${COMPILER_PATH+set}\" || unset COMPILER_PATH || { COMPILER_PATH=; export COMPILER_PATH; }; }; { test -z \"\${GCC_EXEC_PREFIX+set}\" || unset GCC_EXEC_PREFIX || { GCC_EXEC_PREFIX=; export GCC_EXEC_PREFIX; }; }; { test -z \"\${LD_RUN_PATH+set}\" || unset LD_RUN_PATH || { LD_RUN_PATH=; export LD_RUN_PATH; }; }; { test -z \"\${LD_LIBRARY_PATH+set}\" || unset LD_LIBRARY_PATH || { LD_LIBRARY_PATH=; export LD_LIBRARY_PATH; }; }; PATH=/sbin:/bin:/usr/sbin:/usr/bin; export PATH; gcc -g -O2 -Wall -W -o \$progdir/\$file tiff2pdf.o ../libtiff/.libs/libtiff.so ../port/.libs/libport.a -ljpeg -lz -lm -Wl,-rpath -Wl,/home/ec2-user/libtiff/tiff-4.5.0/libtiff/.libs -Wl,-rpath -Wl,/usr/local/lib)"
Anybody able to help me here?