PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.de/

Issue Implementing PDFSharp with Size Limit in AWS Lambda
http://forum.pdfsharp.de/viewtopic.php?f=2&t=4675
Page 1 of 1

Author:  Bhagyashree Teli [ Thu Sep 12, 2024 12:06 pm ]
Post subject:  Issue Implementing PDFSharp with Size Limit in AWS Lambda

I am working on merging multiple PDFs using PDFSharp and need to impose a size limit on the output files. The code works correctly when implemented in a Windows service, but I'm encountering the below error when attempting to run the same code in an AWS Lambda function.
Error: The document was already saved and cannot be modified anymore. Saving a document converts its in memory representation into a PDF file or stream. This can only be done once. After that process the in memory representation is outdated and protected against further modification.
Code:
Code:
public void MergePDFsUsingPDFSharpWithSizeLimitNew(List<string> pdfFiles, string outputDirectory)
{
    const int maxFileSizeKB = 6000; // Maximum file size in KB
    var outputFileIndex = 1;
    var outputFileName = Path.Combine(outputDirectory, $"mergedUsingPdfSharp_{outputFileIndex}.pdf");

    _log.Info($"{nameof(PdfMergerService)}: MergePDFsUsingPDFSharpWithSizeLimitNew: Starting PDF merge...");

    var stopwatch = Stopwatch.StartNew();

    var outputDocument = new PdfDocument();

    foreach (var pdfFile in pdfFiles)
    {
        _log.Info($"{nameof(PdfMergerService)}: Processing file {pdfFile}");
        try
        {
            if (!File.Exists(pdfFile))
                throw new FileNotFoundException($"File not found: {pdfFile}");

            using var inputDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
            for (int i = 0; i < inputDocument.PageCount; i++)
            {
                outputDocument.AddPage(inputDocument.Pages[i]);

                // Save to a temporary stream and check file size
                using var tempStream = new MemoryStream();
                outputDocument.Save(tempStream);
                var fileSizeKB = tempStream.Length / 1024; // Get size in KB

                // If the file size exceeds the limit, save the current PDF and start a new one
                if (fileSizeKB >= maxFileSizeKB)
                {
                    SaveCurrentOutput(outputDocument, outputDirectory, ref outputFileIndex);

                    // Start a new document
                    outputDocument = new PdfDocument();
                }
            }
        }
        catch (Exception ex)
        {
            _log.Error($"{nameof(PdfMergerService)}: Error processing {pdfFile}: {ex.Message}");
        }
    }

    // Save any remaining pages in the output document
    if (outputDocument.PageCount > 0)
    {
        SaveCurrentOutput(outputDocument, outputDirectory, ref outputFileIndex);
    }

    stopwatch.Stop();
    _log.Info($"{nameof(PdfMergerService)}: Merge completed in {stopwatch.ElapsedMilliseconds} ms.");
}

private void SaveCurrentOutput(PdfDocument outputDocument, string outputDirectory, ref int outputFileIndex)
{
    var outputFileName = Path.Combine(outputDirectory, $"mergedUsingPdfSharp_{outputFileIndex}.pdf");

    try
    {
        outputDocument.Save(outputFileName);
        _log.Info($"{nameof(PdfMergerService)}: Output saved to {outputFileName}");
    }
    catch (Exception ex)
    {
        _log.Error($"{nameof(PdfMergerService)}: Error saving merged PDF: {ex.Message}");
    }

    // Increment file index for the next output
    outputFileIndex++;
}

Author:  TH-Soft [ Thu Sep 12, 2024 12:53 pm ]
Post subject:  Re: Issue Implementing PDFSharp with Size Limit in AWS Lambd

Bhagyashree Teli wrote:
The code works correctly when implemented in a Windows service, but I'm encountering the below error when attempting to run the same code in an AWS Lambda function.
AIUI you get the same behaviour under Windows and under AWS.
It's a feature that you can only call "Save()" once.
Start with a new PdfDocument after calling "Save()".

Or call "Save()", then call "Open()" to try and add more pages.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/