Hey guys,
while using the admin pages of my site, when I search for an order number and try to press the "print packaging slip" button, I get an error message saying "Out of Memory" in a red box at the top with no printing. I have attached a copy of both the log and the file causing the issue below:
Thanks in advance :)
here's the logs for the page:
LogID LogTypeID Severity Message Exception IPAddress CustomerID PageURL ReferrerURL CreatedOn
845000 4 11 Out of memory. System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at PdfSharp.Drawing.XImage..ctor(String path)
at PdfSharp.Drawing.XImage.FromFile(String path)
at MigraDoc.Rendering.ImageRenderer.CalculateImageDimensions()
at MigraDoc.Rendering.ImageRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedCell.Format(XGraphics gfx)
at MigraDoc.Rendering.TableRenderer.FormatCells()
at MigraDoc.Rendering.TableRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at NopSolutions.NopCommerce.BusinessLogic.Utils.PDFHelper.PrintPackagingSlipsToPdf(List`1 orderCollection, String filePath) in C:\Development\ToolSparesOnline_NopCommerce\Libraries\Nop.BusinessLogic\Utils\PDFHelper.cs:line 730
at NopSolutions.NopCommerce.Web.Administration.Modules.OrderDetailsControl.BtnPrintPdfPackagingSlip_OnClick(Object sender, EventArgs e) 10.30.3.187 62 https://www.toolsparesonline.com/administration/orderdetails.aspx?orderid=45651 https://www.toolsparesonline.com/administration/OrderDetails.aspx?OrderID=45651 2021-02-17 15:24:50.997
844999 4 11 Out of memory. System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at PdfSharp.Drawing.XImage..ctor(String path)
at PdfSharp.Drawing.XImage.FromFile(String path)
at MigraDoc.Rendering.ImageRenderer.CalculateImageDimensions()
at MigraDoc.Rendering.ImageRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedCell.Format(XGraphics gfx)
at MigraDoc.Rendering.TableRenderer.FormatCells()
at MigraDoc.Rendering.TableRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at NopSolutions.NopCommerce.BusinessLogic.Utils.PDFHelper.PrintPackagingSlipsToPdf(List`1 orderCollection, String filePath) in C:\Development\ToolSparesOnline_NopCommerce\Libraries\Nop.BusinessLogic\Utils\PDFHelper.cs:line 730
at NopSolutions.NopCommerce.Web.Administration.Modules.OrderDetailsControl.BtnPrintPdfPackagingSlip_OnClick(Object sender, EventArgs e) 10.30.3.187 62 https://www.toolsparesonline.com/administration/orderdetails.aspx?orderid=45651 https://www.toolsparesonline.com/administration/OrderDetails.aspx?OrderID=45651 2021-02-17 15:23:53.637
844998 4 11 Out of memory. System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at PdfSharp.Drawing.XImage..ctor(String path)
at PdfSharp.Drawing.XImage.FromFile(String path)
at MigraDoc.Rendering.ImageRenderer.CalculateImageDimensions()
at MigraDoc.Rendering.ImageRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedCell.Format(XGraphics gfx)
at MigraDoc.Rendering.TableRenderer.FormatCells()
at MigraDoc.Rendering.TableRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at NopSolutions.NopCommerce.BusinessLogic.Utils.PDFHelper.PrintPackagingSlipsToPdf(List`1 orderCollection, String filePath) in C:\Development\ToolSparesOnline_NopCommerce\Libraries\Nop.BusinessLogic\Utils\PDFHelper.cs:line 730
at NopSolutions.NopCommerce.Web.Administration.Modules.OrderDetailsControl.BtnPrintPdfPackagingSlip_OnClick(Object sender, EventArgs e) 10.30.3.187 62 https://www.toolsparesonline.com/administration/orderdetails.aspx?orderid=45651 https://www.toolsparesonline.com/administration/OrderDetails.aspx?OrderID=45651 2021-02-17 15:23:52.513
844997 4 11 Out of memory. System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement)
at System.Drawing.Image.FromFile(String filename)
at PdfSharp.Drawing.XImage..ctor(String path)
at PdfSharp.Drawing.XImage.FromFile(String path)
at MigraDoc.Rendering.ImageRenderer.CalculateImageDimensions()
at MigraDoc.Rendering.ImageRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedCell.Format(XGraphics gfx)
at MigraDoc.Rendering.TableRenderer.FormatCells()
at MigraDoc.Rendering.TableRenderer.InitFormat(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TableRenderer.Format(Area area, FormatInfo previousFormatInfo)
at MigraDoc.Rendering.TopDownFormatter.FormatOnAreas(XGraphics gfx, Boolean topLevel)
at MigraDoc.Rendering.FormattedDocument.Format(XGraphics gfx)
at MigraDoc.Rendering.DocumentRenderer.PrepareDocument()
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareDocumentRenderer(Boolean prepareCompletely)
at MigraDoc.Rendering.PdfDocumentRenderer.PrepareRenderPages()
at MigraDoc.Rendering.PdfDocumentRenderer.RenderDocument()
at NopSolutions.NopCommerce.BusinessLogic.Utils.PDFHelper.PrintPackagingSlipsToPdf(List`1 orderCollection, String filePath) in C:\Development\ToolSparesOnline_NopCommerce\Libraries\Nop.BusinessLogic\Utils\PDFHelper.cs:line 730
at NopSolutions.NopCommerce.Web.Administration.Modules.OrderDetailsControl.BtnPrintPdfPackagingSlip_OnClick(Object sender, EventArgs e) 10.30.3.187 62 https://www.toolsparesonline.com/administration/orderdetails.aspx?orderid=45651 https://www.toolsparesonline.com/administration/OrderDetails.aspx?OrderID=45651 2021-02-17 15:23:21.247
844996 0 11 Request timed out. System.Web.HttpException (0x80004005): Request timed out. 10.30.3.187 62 https://www.toolsparesonline.com/administration/orders.aspx https://www.toolsparesonline.com/administration/orders.aspx 2021-02-17 15:19:45.473
and the file causing the issue:
//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.nopCommerce.com/License.aspx.
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
//
// Contributor(s): mb, haydie.
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
using NopSolutions.NopCommerce.BusinessLogic.Configuration.Settings;
using NopSolutions.NopCommerce.BusinessLogic.Directory;
using NopSolutions.NopCommerce.BusinessLogic.Localization;
using NopSolutions.NopCommerce.BusinessLogic.Measures;
using NopSolutions.NopCommerce.BusinessLogic.Media;
using NopSolutions.NopCommerce.BusinessLogic.Orders;
using NopSolutions.NopCommerce.BusinessLogic.Products;
using NopSolutions.NopCommerce.BusinessLogic.Profile;
using NopSolutions.NopCommerce.BusinessLogic.Shipping;
using NopSolutions.NopCommerce.BusinessLogic.Tax;
using NopSolutions.NopCommerce.Common;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Common.Utils.Html;
namespace NopSolutions.NopCommerce.BusinessLogic.Utils
{
/// <summary>
/// Represents a PDF helper
/// </summary>
public partial class PDFHelper
{
#region Methods
/// <summary>
/// Print product collection to PDF
/// </summary>
/// <param name="productCollection"></param>
/// <param name="filePath"></param>
public static void PrintProductsToPdf(List<Product> productCollection, string filePath)
{
if(String.IsNullOrEmpty(filePath))
{
throw new ArgumentNullException("filePath");
}
Document doc = new Document();
Section section = doc.AddSection();
int productNumber = 1;
int prodCount = productCollection.Count;
foreach(var product in productCollection)
{
Paragraph p1 = section.AddParagraph(String.Format("{0}. {1}", productNumber, product.LocalizedName));
p1.Format.Font.Bold = true;
p1.Format.Font.Color = Colors.Black;
section.AddParagraph();
section.AddParagraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(product.LocalizedFullDescription)));
section.AddParagraph();
var pictures = PictureManager.GetPicturesByProductId(product.ProductId);
if (pictures.Count > 0)
{
Table table = section.AddTable();
table.Borders.Visible = false;
table.AddColumn(Unit.FromCentimeter(10));
table.AddColumn(Unit.FromCentimeter(10));
Row row = table.AddRow();
for (int i = 0; i < pictures.Count; i++)
{
int cellNum = i % 2;
var pic = pictures[i];
if (pic != null && pic.LoadPictureBinary() != null && pic.LoadPictureBinary().Length > 0)
{
row.Cells[cellNum].AddImage(PictureManager.GetPictureLocalPath(pic, 200, true));
}
if(i != 0 && i % 2 == 0)
{
row = table.AddRow();
}
}
section.AddParagraph();
}
int pvNum = 1;
foreach(var productVariant in product.ProductVariants)
{
string pvName = String.IsNullOrEmpty(productVariant.LocalizedName) ? LocalizationManager.GetLocaleResourceString("PDFProductCatalog.UnnamedProductVariant") : productVariant.LocalizedName;
section.AddParagraph(String.Format("{0}.{1}. {2}", productNumber, pvNum, pvName));
section.AddParagraph();
if (!String.IsNullOrEmpty(productVariant.LocalizedDescription))
{
section.AddParagraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(productVariant.LocalizedDescription)));
section.AddParagraph();
}
var pic = productVariant.Picture;
if (pic != null && pic.LoadPictureBinary() != null && pic.LoadPictureBinary().Length > 0)
{
section.AddImage(PictureManager.GetPictureLocalPath(pic, 200, true));
}
section.AddParagraph(String.Format("{0}: {1} {2}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.Price"), productVariant.Price, CurrencyManager.PrimaryStoreCurrency.CurrencyCode));
section.AddParagraph(String.Format("{0}: {1}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.SKU"), productVariant.SKU));
if(productVariant.Weight > Decimal.Zero)
{
section.AddParagraph(String.Format("{0}: {1} {2}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.Weight"), productVariant.Weight, MeasureManager.BaseWeightIn.Name));
}
if(productVariant.ManageInventory == (int)ManageInventoryMethodEnum.ManageStock)
{
section.AddParagraph(String.Format("{0}: {1}", LocalizationManager.GetLocaleResourceString("PDFProductCatalog.StockQuantity"), productVariant.StockQuantity));
}
section.AddParagraph();
pvNum++;
}
productNumber++;
if(productNumber <= prodCount)
{
section.AddPageBreak();
}
}
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = doc;
renderer.RenderDocument();
renderer.PdfDocument.Save(filePath);
}
/// <summary>
/// Print an order to PDF
/// </summary>
/// <param name="order">Order</param>
/// <param name="languageId">Language identifier</param>
/// <param name="filePath">File path</param>
public static void PrintOrderToPdf(Order order, int languageId, string filePath)
{
if(order == null)
throw new ArgumentNullException("order");
if(String.IsNullOrEmpty(filePath))
throw new ArgumentNullException("filePath");
Language lang = LanguageManager.GetLanguageById(languageId);
if (lang == null)
throw new NopException("Language could not be loaded");
Document doc = new Document();
Section sec = doc.AddSection();
Table table = sec.AddTable();
table.Borders.Visible = false;
bool logoExists = File.Exists(PDFHelper.LogoFilePath);
table.AddColumn(Unit.FromCentimeter(10));
if(logoExists)
{
table.AddColumn(Unit.FromCentimeter(10));
}
Row ordRow = table.AddRow();
int rownum = logoExists ? 1 : 0;
Paragraph p1 = ordRow[rownum].AddParagraph(String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Order#", languageId), order.OrderId));
p1.Format.Font.Bold = true;
p1.Format.Font.Color = Colors.Black;
ordRow[rownum].AddParagraph(SettingManager.StoreUrl.Trim(new char[] { '/' })).AddHyperlink(SettingManager.StoreUrl, HyperlinkType.Url);
ordRow[rownum].AddParagraph(String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.OrderDate", languageId), DateTimeHelper.ConvertToUserTime(order.CreatedOn, DateTimeKind.Utc).ToString("D")));
if(File.Exists(PDFHelper.LogoFilePath))
{
ordRow[0].AddImage(PDFHelper.LogoFilePath);
}
var addressTable = sec.AddTable();
if(order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
{
addressTable.AddColumn(Unit.FromCentimeter(9));
addressTable.AddColumn(Unit.FromCentimeter(9));
}
else
{
addressTable.AddColumn(Unit.FromCentimeter(18));
}
addressTable.Borders.Visible = false;
Row row = addressTable.AddRow();
//billing info
row.Cells[0].AddParagraph();
Paragraph p2 = row.Cells[0].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.BillingInformation", languageId));
p2.Format.Font.Bold = true;
p2.Format.Font.Color = Colors.Black;
if(!String.IsNullOrEmpty(order.BillingCompany))
{
row.Cells[0].AddParagraph(" " + order.BillingCompany);
}
row.Cells[0].AddParagraph(" " + order.BillingFullName);
row.Cells[0].AddParagraph(" " + order.BillingAddress1);
if(!String.IsNullOrEmpty(order.BillingAddress2))
row.Cells[0].AddParagraph(" " + order.BillingAddress2);
row.Cells[0].AddParagraph(" " + order.BillingCity);
row.Cells[0].AddParagraph(" " + order.BillingStateProvince);
row.Cells[0].AddParagraph(" " + order.BillingZipPostalCode);
row.Cells[0].AddParagraph(" " + order.BillingCountry);
row.Cells[0].AddParagraph();
row.Cells[0].AddParagraph(" " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Phone", languageId), order.BillingPhoneNumber));
if (!String.IsNullOrEmpty(order.BillingFaxNumber))
row.Cells[0].AddParagraph(" " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Fax", languageId), order.BillingFaxNumber));
row.Cells[0].AddParagraph(" " + String.Format("{0}", order.BillingEmail));
//VAT number
if (!String.IsNullOrEmpty(order.VatNumber))
{
row.Cells[0].AddParagraph(" " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.VATNumber", languageId), order.VatNumber));
}
row.Cells[0].AddParagraph();
//shipping info
if(order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
{
row.Cells[1].AddParagraph();
Paragraph p3 = row.Cells[1].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.ShippingInformation", languageId));
p3.Format.Font.Bold = true;
p3.Format.Font.Color = Colors.Black;
if(!String.IsNullOrEmpty(order.ShippingCompany))
row.Cells[1].AddParagraph(" " + order.ShippingCompany);
row.Cells[1].AddParagraph(" " + order.ShippingFullName);
row.Cells[1].AddParagraph(" " + order.ShippingAddress1);
if(!String.IsNullOrEmpty(order.ShippingAddress2))
row.Cells[1].AddParagraph(" " + order.ShippingAddress2);
row.Cells[1].AddParagraph(" " + order.ShippingCity);
row.Cells[1].AddParagraph(" " + order.ShippingZipPostalCode);
row.Cells[1].AddParagraph(" " + order.ShippingStateProvince);
row.Cells[1].AddParagraph(" " + order.ShippingCountry);
row.Cells[1].AddParagraph();
row.Cells[1].AddParagraph(" " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Phone", languageId), order.ShippingPhoneNumber));
if (!String.IsNullOrEmpty(order.ShippingFaxNumber))
row.Cells[1].AddParagraph(" " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.Fax", languageId), order.ShippingFaxNumber));
row.Cells[1].AddParagraph(" " + String.Format("{0}", order.ShippingEmail));
row.Cells[1].AddParagraph();
row.Cells[1].AddParagraph(" " + String.Format(LocalizationManager.GetLocaleResourceString("PDFInvoice.ShippingMethod", languageId), order.ShippingMethod));
row.Cells[1].AddParagraph();
}
sec.AddParagraph();
//products
Paragraph p4 = sec.AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.Product(s)", languageId));
p4.Format.Font.Bold = true;
p4.Format.Font.Color = Colors.Black;
sec.AddParagraph();
var productCollection = order.OrderProductVariants;
var tbl = sec.AddTable();
tbl.Borders.Visible = true;
tbl.Borders.Width = 1;
tbl.AddColumn(Unit.FromCentimeter(5));
tbl.AddColumn(Unit.FromCentimeter(2));
tbl.AddColumn(Unit.FromCentimeter(2));
tbl.AddColumn(Unit.FromCentimeter(4));
tbl.AddColumn(Unit.FromCentimeter(1));
tbl.AddColumn(Unit.FromCentimeter(4));
Row header = tbl.AddRow();
header.Cells[0].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductName", languageId));
header.Cells[0].Format.Alignment = ParagraphAlignment.Center;
header.Cells[1].AddParagraph("SKU");
header.Cells[1].Format.Alignment = ParagraphAlignment.Center;
header.Cells[2].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.MPN", languageId));
header.Cells[2].Format.Alignment = ParagraphAlignment.Center;
header.Cells[3].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductPrice", languageId));
header.Cells[3].Format.Alignment = ParagraphAlignment.Center;
header.Cells[4].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductQuantity", languageId));
header.Cells[4].Format.Alignment = ParagraphAlignment.Center;
header.Cells[5].AddParagraph(LocalizationManager.GetLocaleResourceString("PDFInvoice.ProductTotal", languageId));
header.Cells[5].Format.Alignment = ParagraphAlignment.Center;
for(int i = 0; i < productCollection.Count; i++)
{
var orderProductVariant = productCollection[i];
int rowNum = i + 1;
Row prodRow = tbl.AddRow();
string name = String.Format("Not available. Id={0}", orderProductVariant.ProductVariantId);
var pv = ProductManager.GetProductVariantById(orderProductVariant.ProductVariantId);
if(pv != null)
{
name = pv.GetLocalizedFullProductName(languageId);
}
prodRow.Cells[0].AddParagraph(name);
Paragraph p5 = prodRow.Cells[0].AddParagraph(HtmlHelper.ConvertHtmlToPlainText(orderProductVariant.AttributeDescription, true));
p5.Format.Font.Italic = true;
prodRow.Cells[0].Format.Alignment = ParagraphAlignment.Left;
string unitPrice = string.Empty;
switch(order.CustomerTaxDisplayType)
{
case TaxDisplayTypeEnum.ExcludingTax:
unitPrice = PriceHelper.FormatPrice(orderProductVariant.UnitPriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, fa