Generate Invoice PDF Containing Graphical Logo

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
1. nopCommerce version - 4.6.0 (download)
2. Expected behavior - Create Invoice PDF with company logo
3. Actual behavior - Error - "The data buffer was empty." and no invoice PDF
4. Steps to reproduce the problem

a. Configuration\Settings\Pdf - Check PDF logo, upload a file (check blob file exist nnnnnnn_filename_thumbsize.fileext)
b. Click Save
c. Clear Cache
d. Sales\Orders\Order - Invoice (PDF) - receive error (check blob file exist nnnnnnn_filename.fileext)
e. Exception

System.ArgumentException: The data buffer was empty.
   at SkiaSharp.SKImage.FromEncodedData(Byte[] data)
   at QuestPDF.Fluent.ImageExtensions.Image(IContainer parent, Byte[] imageData, ImageScaling scaling)
   at Nop.Services.Common.Pdf.InvoiceDocument.<ComposeHeader>b__4_1(RowDescriptor row) in D:\users\***\source\repos\***\****\src\Libraries\Nop.Services\Common\Pdf\InvoiceDocument.cs:line 124

5. Any private modifications you made to your nopCommerce

None
1 year ago
Hi! I couldn't reproduce the issue. What image format did you try to upload? I've tested png/webp and it works fine.
1 year ago
Hi,

Thanks for the quick response.  I tried uploading various valid png and jpeg images, but all had the same issue.  My images are stored in the file system (cloud) - Media Settings/Common - Pictures are stored into... file system: checked.

The Source.LogoData byte array is empty when it attempts to compose the header.  Going back up the stack, the PrintOrderToPdfAsync method attempts to load logoFilePath (http://bloblocation/content/filename.fileext) using System File object, but will fail as file does not exist on system, but in the cloud.  

Thanks,
1 year ago
I have a custom store logo.png set to print on pdf.

For some reason the thumb file was corrupted and 0 bytes.

nopCommerce\src\Presentation\Nop.Web\wwwroot\images\thumbs\0000007_Logo_150.png (0bytes)

The following is line is throwing because data array is 0.

\nopCommerce\src\Libraries\Nop.Services\Common\Pdf\InvoiceDocument.cs

Current:
  if (Source.LogoData is not null)
                    logoContainer.Image(Source.LogoData, ImageScaling.FitArea);

Proposed Change: (Doesn't solve problem of logo.png not printing)
if (Source.LogoData is not null && Source.LogoData.Length != 0)
                    logoContainer.Image(Source.LogoData, ImageScaling.FitArea);



Full Error Message:

System.ArgumentException: The data buffer was empty.
   at SkiaSharp.SKImage.FromEncodedData(Byte[] data)
   at QuestPDF.Fluent.ImageExtensions.Image(IContainer parent, Byte[] imageData, ImageScaling scaling)
   at Nop.Services.Common.Pdf.InvoiceDocument.<ComposeHeader>b__4_1(RowDescriptor row)
   at QuestPDF.Fluent.RowExtensions.Row(IContainer element, Action`1 handler)
   at Nop.Services.Common.Pdf.InvoiceDocument.ComposeHeader(IContainer container)
   at QuestPDF.Fluent.ElementExtensions.Element[TParent](TParent parent, Action`1 handler)
   at Nop.Services.Common.Pdf.InvoiceDocument.<Compose>b__11_0(PageDescriptor page)
   at QuestPDF.Fluent.PageExtensions.Page(IDocumentContainer document, Action`1 handler)
   at Nop.Services.Common.Pdf.InvoiceDocument.Compose(IDocumentContainer container)
   at QuestPDF.Drawing.DocumentGenerator.RenderDocument[TCanvas](TCanvas canvas, IDocument document)
   at QuestPDF.Drawing.DocumentGenerator.GeneratePdf(Stream stream, IDocument document)
   at QuestPDF.Fluent.GenerateExtensions.GeneratePdf(IDocument document, Stream stream)
   at Nop.Services.Common.PdfService.PrintOrderToPdfAsync(Stream stream, Order order, Language language, Store store, Vendor vendor)
   at Nop.Services.Common.PdfService.PrintOrderToPdfAsync(Stream stream, Order order, Language language, Store store, Vendor vendor)
   at Nop.Services.Common.PdfService.PrintOrdersToPdfAsync(Stream stream, IList`1 orders, Language language, Vendor vendor)
   at Nop.Services.Common.PdfService.PrintOrdersToPdfAsync(Stream stream, IList`1 orders, Language language, Vendor vendor)
   at Nop.Services.Common.PdfService.PrintOrdersToPdfAsync(Stream stream, IList`1 orders, Language language, Vendor vendor)
at Nop.Web.Areas.Admin.Controllers.OrderController.PdfInvoiceSelected(String selectedIds) in C:\Users\Development\nopCommerce\src\Presentation\Nop.Web\Areas\Admin\Controllers\OrderController.cs:line 1053
   at Nop.Web.Areas.Admin.Controllers.OrderController.PdfInvoiceSelected(String selectedIds) in C:\Users\Development\nopCommerce\src\Presentation\Nop.Web\Areas\Admin\Controllers\OrderController.cs:line 1055


You may want to consider a byte[0] check on the SaveThumgAsync in PictureServices.cs line 273:

protected virtual async Task SaveThumbAsync(string thumbFilePath, string thumbFileName, string mimeType, byte[] binary)
        {
            //ensure \thumb directory exists
            var thumbsDirectoryPath = _fileProvider.GetAbsolutePath(NopMediaDefaults.ImageThumbsPath);
            _fileProvider.CreateDirectory(thumbsDirectoryPath);

            //save
            await _fileProvider.WriteAllBytesAsync(thumbFilePath, binary);
        }
1 year ago
\nopCommerce\src\Libraries\Nop.Services\Common\Pdf\InvoiceDocument.cs

Current:
  if (Source.LogoData is not null)
                    logoContainer.Image(Source.LogoData, ImageScaling.FitArea);

Proposed Change: (Doesn't solve problem of logo.png not printing)
if (Source.LogoData?.Length > 0)
                    logoContainer.Image(Source.LogoData, ImageScaling.FitArea);
1 year ago
We'll check this again. Here is a work item.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.