我正在开发一个AWS Lambda应用程序,该应用程序需要获取TIFF文件并将其转换为PDF。我正在大量使用ImageMagick,所以最简单的方法是:convert input.tif output.pdf
。在我的Mac环境中可以正常工作,但在Lambda环境中无法转换为真正的PDF。
基于Lambda构建的ImageMagick似乎不支持PDF。如果我在Lambda环境中运行convert -list format
,则没有PDF条目。这是我的测试Lambda函数:
const im = require('imagemagick');
const fs = require('fs');
exports.handler = (event, context, callback) => {
var inputFileName = 'input.tif';
var imagesPath = 'assets/images';
var outputFile = '/tmp/output.pdf';
var args = [
imagesPath+'/'+inputFileName,
'-format',
'pdf',
outputFile
];
im.convert(args,
function(err, stdout, stderr){
if (err) throw err;
console.log('stdout:', stdout);
var imageRef = fs.readFileSync(outputFile);
callback(null, {
statusCode: 200,
headers: {
'Content-Type': 'application/pdf',
'Content-Disposition': 'attachment; filename=output.pdf'
},
body: imageRef.toString('base64'),
isBase64Encoded: true
});
});
}
当我运行identify output.pdf
(即下载的文件)时,该文件被报告为TIFF文件:
/Users/myuser/Downloads/output.pdf TIFF 517x243 517x243+0+0 8-bit CMYK 1.1314MiB 0.000u 0:00.009
因此ImageMagick似乎只是将其作为TIFF文件传递。
我尝试使用tiff2pdf-在本地安装;不确定Lambda-甚至在我的Mac上也不起作用。我收到一条错误消息:
tiff2pdf: No support for /path/to/input.tif with 5 samples per pixel.
答案 0 :(得分:0)
AWS Lambada现在支持在C#中创建函数。 Foxit PDF SDK 6.4支持将TIFF转换为PDF。
此解决方案需要Foxit PDF SDK 6.4 for .net。 您可以在以下链接中找到对评估包的请求:https://developers.foxitsoftware.com/pdf-sdk/free-trial
您可以添加fsdk_dotnet.dll作为对Visual Studio“ AWS Lambda Project(.Net Core-C#)”的引用。 fsdk_dotnet.dll位于评估包的lib目录中。 完成后,您可以添加以下using语句。
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
为了您的功能,它看起来像这样。
public string TiffToPDF(string input, string output)
{
//the sn and key value are license/evaluation values. This is provided with the Foxit PDF SDK evaluation package in the lib directory.
string sn = "SNValue"; //the SN value provided in the evaluation package at lib\gsdk_sn.txt
string key = "SignKeyValue"; //the Sign value provided in evaluation package at lib\gsdk_key.txt
ErrorCode error_code;
try
{
error_code = Library.Initialize(sn, key); //Unlocks the library to be used. Make sure you update the sn and key file accordingly.
if (error_code != ErrorCode.e_ErrSuccess)
{
return error_code.ToString();
}
PDFDoc doc = new PDFDoc(); //Creates a PDF document object
foxit.common.Image image = new foxit.common.Image(input); //Create a image object from the text file
int pageIndex = doc.GetPageCount(); //Get the page count
PDFPage page = doc.InsertPage(pageIndex, image.GetWidth(), image.GetHeight()); //Adds a blank PDF page that matches the images height and width to the PDF document object
page.StartParse((int)PDFPage.ParseFlags.e_ParsePageNormal, null, false); //Parsing is required here. Just do it.
page.AddImage(image, 0, new PointF(0, 0), page.GetWidth(), page.GetHeight(), true); //Adds a image to the PDF page
doc.SaveAs(output, (int)PDFDoc.SaveFlags.e_SaveFlagIncremental); //Save the new PDF to the output path
image.Dispose(); //clean up the cache data used to create the image object
page.Dispose(); //clean up the cache data used to create the PDF page
doc.Dispose(); //clean up the cache data used to create the PDF Document object
Library.Release(); //clean up the cache data used by the Foxit PDF SDK library
}
catch (foxit.PDFException e)
{
return e.Message; //If successful this will return the "E_ERRSUCCESS." Please check out the headers for other error codes.
}
catch (Exception e)
{
return e.Message;
}
return error_code.ToString().ToUpper();
}
fsdk_dotnet.dll引用位于lib目录中的fsdk.dll文件。您必须确保将fsdk.dll正确输出到输出目录,以使引用正确。