Deployed AWS Lambda on convert HTML to PDF throwing exception "No such file or directory"

I was trying the nuget package Syncfusion.HtmlToPdfConverter.QtWebKit.Net.Coreon a console application and it was working perfectly!
(Has all the HTML perfectly placed and the expected header and footer).

I placed this on deployed lambda function AWS and the PdfDocument Convert method keeps throwing "No such file or directory".
This is not a code problem because all our previous files and templates that was used before was successfully retrieved (and I also tested this on deployed code).

Here is the code:

[ExcludeFromCodeCoverage]
    public class SyncfusionWrapper : ISyncfusionWrapper
    {
        ///
        /// The path provider.
        ///
        private readonly IPathProvider pathProvider;

        ///
        /// Initializes a new instance of the class.
        ///
        /// The path provider.
        public SyncfusionWrapper(IPathProvider pathProvider)
        {
            this.pathProvider = pathProvider;
        }

        ///
        /// Converts the HTML files to PDF bytes.
        ///
        /// The HTML text files.
        /// The binary file path for SyncFusion.
        /// The HTML header file path.
        /// The HTML footer file path.
        /// The PDF bytes converted from HTML files.
        public byte[] ConvertHtmlToPdfBytes(List htmlFiles, string webKitPath, string htmlHeaderPath = null, string htmlFooterPath = null)
        {
            byte[] result = null;

            if (htmlFiles != null && htmlFiles.Count > 0)
            {
                var documents = new List();

                for (var ctr = 0; ctr < htmlFiles.Count; ctr++)
                {
                    documents.Add(this.GetLoadedDocument(htmlFiles[ctr], webKitPath, htmlHeaderPath, htmlFooterPath));

                    if (ctr != 0)
                    {
                        //// FIRST DOCUMENT SHOULD EXTEND OTHER DOCUMENTS (IF ANY)
                        documents[0].ImportPageRange(documents[ctr], 0, documents[ctr].Pages.Count - 1);
                    }
                }

                var stream = documents[0].SaveAndGetStream();
                result = stream.ToArray();
            }

            return result;
        }

        ///
        /// Gets the .
        ///
        /// The HTML text file.
        /// The binary file path for SyncFusion.
        /// The HTML header file path.
        /// The HTML footer file path.
        /// The created .
        private PdfLoadedDocument GetLoadedDocument(string htmlFile, string webKitPath, string htmlHeaderPath = null, string htmlFooterPath = null)
        {
            var settings = new WebKitConverterSettings()
            {
                WebKitPath = webKitPath,
                Margin = new PdfMargins() { Top = 40, Left = 30, Right = 40, Bottom = 50 },
            };

            if (!string.IsNullOrWhiteSpace(htmlHeaderPath))
            {
                settings.Margin.Top = 10;
                settings.PdfHeader = this.GetHeaderFooterPdfPageTemplate(htmlHeaderPath, webKitPath);
            }

            if (!string.IsNullOrWhiteSpace(htmlFooterPath))
            {
                settings.Margin.Bottom = 10;
                settings.PdfFooter = this.GetHeaderFooterPdfPageTemplate(htmlFooterPath, webKitPath);
            }

            var converter = settings.ToHtmlPdfConverter();

            PdfDocument document = converter.Convert(htmlFile, string.Empty);
            document.PageSettings.Size = PdfPageSize.A4;

            var docStream = document.SaveAndGetStream();
            var result = new PdfLoadedDocument(docStream);
            return result;
        }

        ///
        /// Gets the header or footer PDF page template.
        ///
        /// The HTML file path.
        /// The binary file path for SyncFusion.
        /// The header or footer PDF page template.
        private PdfPageTemplateElement GetHeaderFooterPdfPageTemplate(string htmlFilePath, string webKitPath)
        {
            var settings = new WebKitConverterSettings()
            {
                WebKitPath = webKitPath,
                PdfPageSize = new SizeF(PdfPageSize.A4.Width, 20),
                Orientation = PdfPageOrientation.Landscape,
                SinglePageLayout = SinglePageLayout.FitWidth,
                WebKitViewPort = new Size(1024, 0),
            };

            var converter = settings.ToHtmlPdfConverter();
            var document = converter.Convert(htmlFilePath);

            var firstPageSize = document.Pages[0].GetClientSize();
            var bounds = new RectangleF(0, 0, firstPageSize.Width, firstPageSize.Height);

            var header = new PdfPageTemplateElement(bounds);
            header.Graphics.DrawPdfTemplate(document.Pages[0].CreateTemplate(), bounds.Location, bounds.Size);

            return header;
        }
    }

I think that there is something wrong with the Convert method, because the other Lambda tool I used to test, using Nuget package Syncfusion.Pdf.Net.Core, worked on deployed AWS, here is code:

PdfDocument document = new PdfDocument();

                //Add a page to the document
                PdfPage page = document.Pages.Add();

                //Create PDF graphics for the page
                PdfGraphics graphics = page.Graphics;

                //Set the standard font
                PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);

                //Draw the text
                graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));

                string imagePath = Path.GetFullPath(@"Data/logo.jpg");

                //Load the image from the disk
                FileStream imageStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);

                PdfBitmap image = new PdfBitmap(imageStream);
                //Draw the image
                graphics.DrawImage(image, 30, 30, 100, 25);

                //Save the document into stream
                MemoryStream stream = new MemoryStream();

                //Save the PDF document  
                document.Save(stream);
                document.Close();

                File.WriteAllBytes("test.pdf", stream.ToArray());
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("test.pdf") { UseShellExecute = true });

                return Convert.ToBase64String(stream.ToArray());


1 Reply

SL Sowmiya Loganathan Syncfusion Team February 21, 2020 12:43 PM UTC

Hi Stephanie,   
   
The reported exception may occur due to missing dependency packages in the AWS lambda function. Please find the pre-requisites for converting HTML to PDF in the Linux environment from the below link. Kindly install the pre-requisites in the AWS lambda function and try the conversion and let us know the result. But PDF document creation does not have any pre-requisites, so it will work properly in AWS lambda function.    
   
We already tried to install the packages in AWS lambda, but we are unable to install the packages successfully. So, we have raised a query in the AWS forum, but we are not getting any solution for this. Please find the link below,    
 
Regards, 
Sowmiya Loganathan 


Loader.
Up arrow icon