IronPdf / Iron-Pdf-Documentation

ReadTheDocs Build Files for IronPDF C#/.Net PDF Library http://ironpdf.com
8 stars 2 forks source link

Exception Converting Images to PDF #26

Open AlejandroHV opened 1 year ago

AlejandroHV commented 1 year ago

Morning,

I've been working on a C# corporate application using the IronPDF nuget package on version 2023.4.4. We decided to upgrade the version of the library to the latests one: 2023.6.10 because there was an error with some PDF's. Updating solved the issue with the PDF's but it caused an exception with creation of PDF based on an image.

Exception Message: Value cannot be null. (Parameter 'source') Stack Trace: at System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at IronPdf.Engines.Chrome.LocalChromeClient.RenderPDFFromImages(List1 image_paths_in, IEnumerable1 images, ImageBehavior behavior, ChromePdfRenderOptions RenderingOptions) at IronPdf.ImageToPdfConverter.ImageToPdf(IEnumerable1 images, ImageBehavior behavior, ChromePdfRenderOptions options) at IronPDFTest.Program.MergePDFs() in C:\Users\aleja\source\repos\IronPDFTest\IronPDFTest\Program.cs:line 43 at IronPDFTest.Program.Main(String[] args) in C:\Users\aleja\source\repos\IronPDFTest\IronPDFTest\Program.cs:line 22

I even created an example console application and got the same problem. The stack trace is for this console application. The code we are using is the following:

`using (FileStream fs = File.Open("./azul.png", FileMode.Open, FileAccess.Read)) { int length = (int)fs.Length; buffer = new byte[length]; int count;
int sum = 0;

            while ((count = fs.Read(buffer, sum, length - sum)) > 0)
                sum += count;  

            var a = new AnyBitmap(buffer);
                var pdfimage2 = ImageToPdfConverter.ImageToPdf(new List<AnyBitmap> { a });
            var pdfimage = ImageToPdfConverter.ImageToPdf(a);

        }`

It has something to do with the overload of the method "ImageToPdf" with AnyBitMap as parameters , either the list or just one. I tried with the overload to send the image paths and did not receive an exception. In the version 2023.4.4 it worked appropriately. Thank you

Thyrador commented 1 year ago

The issue lies in public IPdfDocumentId RenderPDFFromImages which tries to resolve image_paths_in: List<string> all = image_paths_in.ToList<string>().FindAll((Predicate<string>) (f => !File.Exists(f))); whereas

public static PdfDocument ImageToPdf(
      IEnumerable<AnyBitmap> images,
      ImageBehavior behavior = ImageBehavior.CenteredOnPage,
      ChromePdfRenderOptions options = null)
    {
      return new ChromePdfRenderer().ChromeClient.RenderPDFFromImages((List<string>) null, images.Select<AnyBitmap, (byte[], string)>((Func<AnyBitmap, (byte[], string)>) (i => (i.GetBytes(), i.MimeType))), behavior, options).ToPdfDocument;
    }

passes a List\<string> as null, which ultimately results in the exception you posted. This is an issue within IronPDF and needs to fixed by them.

If you don't need to use AnyBitmap/Streams at all and can access the files directly from the disk, then you can go this route:

var sl = new List<string>();
for (var i = 0; i < 4; i++)
{
    sl.Add($@"P:\Test\base_{i}.png");
};
ImageToPdfConverter.ImageToPdf(sl).SaveAs($@"P:\Test\base_sl_final_{DateTime.Now.Ticks}.pdf");

Since the list is populated at this point it won't throw the error and therefore works. Hope it helps until it is fixed.

Also to receive help you should open a support ticket on their website: How to Make an Engineering Support Request for IronPDF

Thyrador commented 1 year ago

@AlejandroHV Just a small heads up for you: your issue is resolved in IronPDF v2023.7.4

This example code worked for me:

var sl = new List<string>();
var abl = new List<AnyBitmap>();
for (var i = 0; i < 4; i++)
{
    sl.Add($@"P:\Test\base_{i}.png");
    abl.Add(new AnyBitmap(File.ReadAllBytes($@"P:\Test\base_{i}.png")));
}

ImageToPdfConverter.ImageToPdf(sl).SaveAs($@"P:\Test\base_sl_final_{DateTime.Now.Ticks}.pdf");
ImageToPdfConverter.ImageToPdf(abl, ImageBehavior.FitToPageAndMaintainAspectRatio).SaveAs($@"P:\Test\base_abl_final_{DateTime.Now.Ticks}.pdf");