VahidN / iTextSharp.LGPLv2.Core

iTextSharp.LGPLv2.Core is an unofficial port of the last LGPL version of the iTextSharp (V4.1.6) to .NET Core
Other
615 stars 154 forks source link

Verify if the image is encoded in base64 before treating it as a URL. #129

Closed irico-epi closed 11 months ago

irico-epi commented 11 months ago

When creating a PDF from HTML, if the 'img' tag contains a base64-encoded image, it attempts to convert it into a URL. In the case of small images, it works well, but for larger images, it generates an exception. By verifying in advance if the 'img' tag contains a base64 image and handling it specifically, the issue is resolved.

VahidN commented 11 months ago

This PR is not necessary, because this case after converting it to a URL, will be handled, and if you think it's not, show us a sample about it. Post an exception, something, ... Also follow this sample.

irico-epi commented 11 months ago

The problem is that URI has a limitation on the maximum string length, and for very large images, it throws an exception on the URI constructor. I'm attaching a small sample code to reproduce the error, as well as a screenshot of the error it throws.

image ConvertToBase64

Sample code:

` try { //I've removed part of the base64 image because it was too large and badly formatted. I'm attaching the image so that you can use it for testing. string filename = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBXAFcAAD/4QAiRXhpZg ......";

  Uri url = ConsoleApp1.Utilities.ToUrl(filename);

  if (url == null)
  {
      throw new ArgumentNullException(nameof(url));
  }

  // Add support for base64 encoded images.
  if (url.Scheme == "data")
  {
      Console.WriteLine("It is base64 image");
  }

} catch (Exception ex) { Console.WriteLine(ex.ToString()); } `

VahidN commented 11 months ago

OK. I will add it later without duplicating the same code block twice.