fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Nop.Services.Stores;
  6. using iTextSharp.text;
  7. using iTextSharp.text.pdf;
  8. using Nop.Core;
  9. using Nop.Core.Domain.Catalog;
  10. using Nop.Core.Domain.Common;
  11. using Nop.Core.Domain.Directory;
  12. using Nop.Core.Domain.Orders;
  13. using Nop.Core.Domain.Shipping;
  14. using Nop.Core.Domain.Tax;
  15. using Nop.Core.Html;
  16. using Nop.Services.Catalog;
  17. using Nop.Services.Directory;
  18. using Nop.Services.Helpers;
  19. using Nop.Services.Localization;
  20. using Nop.Services.Media;
  21. using Nop.Services.Orders;
  22. using Nop.Services.Payments;
  23. using System.Globalization;
  24.  
  25. namespace Nop.Services.Common
  26. {
  27. /// <summary>
  28. /// PDF service
  29. /// </summary>
  30. public partial class PdfService : IPdfService
  31. {
  32. #region Fields
  33.  
  34. private readonly ILocalizationService _localizationService;
  35. private readonly ILanguageService _languageService;
  36. private readonly IWorkContext _workContext;
  37. private readonly IOrderService _orderService;
  38. private readonly IPaymentService _paymentService;
  39. private readonly IDateTimeHelper _dateTimeHelper;
  40. private readonly IPriceFormatter _priceFormatter;
  41. private readonly ICurrencyService _currencyService;
  42. private readonly IMeasureService _measureService;
  43. private readonly IPictureService _pictureService;
  44. private readonly IProductService _productService;
  45. private readonly IProductAttributeParser _productAttributeParser;
  46. private readonly IStoreService _storeService;
  47. private readonly IStoreContext _storeContext;
  48. private readonly IWebHelper _webHelper;
  49.  
  50. private readonly CatalogSettings _catalogSettings;
  51. private readonly CurrencySettings _currencySettings;
  52. private readonly MeasureSettings _measureSettings;
  53. private readonly PdfSettings _pdfSettings;
  54. private readonly TaxSettings _taxSettings;
  55. private readonly AddressSettings _addressSettings;
  56.  
  57. #endregion
  58.  
  59. #region Ctor
  60.  
  61. public PdfService(ILocalizationService localizationService,
  62. ILanguageService languageService,
  63. IWorkContext workContext,
  64. IOrderService orderService,
  65. IPaymentService paymentService,
  66. IDateTimeHelper dateTimeHelper, IPriceFormatter priceFormatter,
  67. ICurrencyService currencyService, IMeasureService measureService,
  68. IPictureService pictureService, IProductService productService,
  69. IProductAttributeParser productAttributeParser, IStoreService storeService,
  70. IStoreContext storeContext, IWebHelper webHelper,
  71. CatalogSettings catalogSettings, CurrencySettings currencySettings,
  72. MeasureSettings measureSettings, PdfSettings pdfSettings, TaxSettings taxSettings,
  73. AddressSettings addressSettings)
  74. {
  75. this._localizationService = localizationService;
  76. this._languageService = languageService;
  77. this._workContext = workContext;
  78. this._orderService = orderService;
  79. this._paymentService = paymentService;
  80. this._dateTimeHelper = dateTimeHelper;
  81. this._priceFormatter = priceFormatter;
  82. this._currencyService = currencyService;
  83. this._measureService = measureService;
  84. this._pictureService = pictureService;
  85. this._productService = productService;
  86. this._productAttributeParser = productAttributeParser;
  87. this._storeService = storeService;
  88. this._storeContext = storeContext;
  89. this._webHelper = webHelper;
  90. this._currencySettings = currencySettings;
  91. this._catalogSettings = catalogSettings;
  92. this._measureSettings = measureSettings;
  93. this._pdfSettings = pdfSettings;
  94. this._taxSettings = taxSettings;
  95. this._addressSettings = addressSettings;
  96. }
  97.  
  98. #endregion
  99.  
  100. #region Utilities
  101.  
  102. protected virtual Font GetFont()
  103. {
  104. //nopCommerce supports unicode characters
  105. //nopCommerce uses Free Serif font by default (~/App_Data/Pdf/FreeSerif.ttf file)
  106. //It was downloaded from http://s...content-available-to-author-only...u.org/projects/freefont
  107. string fontPath = Path.Combine(_webHelper.MapPath("~/App_Data/Pdf/"), _pdfSettings.FontFileName);
  108. var baseFont = BaseFont.CreateFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
  109. var font = new Font(baseFont, 10, Font.NORMAL);
  110. return font;
  111. }
  112.  
  113.  
  114. #endregion
  115.  
  116. #region Methods
  117.  
  118. /// <summary>
  119. /// Print an order to PDF
  120. /// </summary>
  121. /// <param name="stream">Stream</param>
  122. /// <param name="orders">Orders</param>
  123. /// <param name="languageId">Language identifier; 0 to use a language used when placing an order</param>
  124. public virtual void PrintOrdersToPdf(Stream stream, IList<Order> orders, int languageId = 0)
  125. {
  126. if (stream == null)
  127. throw new ArgumentNullException("stream");
  128.  
  129. if (orders == null)
  130. throw new ArgumentNullException("orders");
  131.  
  132. var lang = _languageService.GetLanguageById(languageId);
  133. if (lang == null)
  134. throw new ArgumentException(string.Format("Cannot load language. ID={0}", languageId));
  135.  
  136. var pageSize = PageSize.A4;
  137.  
  138. if (_pdfSettings.LetterPageSizeEnabled)
  139. {
  140. pageSize = PageSize.LETTER;
  141. }
  142.  
  143.  
  144. var doc = new Document(pageSize);
  145. PdfWriter.GetInstance(doc, stream);
  146. doc.Open();
  147.  
  148. //fonts
  149.  
  150.  
  151. var titleFont = GetFont();
  152. titleFont.SetStyle(Font.BOLD);
  153. titleFont.Color = BaseColor.BLACK;
  154. var font = GetFont();
  155. var attributesFont = GetFont();
  156. attributesFont.SetStyle(Font.ITALIC);
  157.  
  158. int ordCount = orders.Count;
  159. int ordNum = 0;
  160.  
  161. foreach (var order in orders)
  162. {
  163. if (languageId == 0)
  164. {
  165. lang = _languageService.GetLanguageById(order.CustomerLanguageId);
  166. if (lang == null || !lang.Published)
  167. lang = _workContext.WorkingLanguage;
  168. }
  169.  
  170. #region Header
  171.  
  172. //logo
  173. var logoPicture = _pictureService.GetPictureById(_pdfSettings.LogoPictureId);
  174. var logoExists = logoPicture != null;
  175.  
  176. //header
  177. var headerTable = new PdfPTable(logoExists ? 2 : 1);
  178. headerTable.WidthPercentage = 100f;
  179. if (logoExists)
  180. headerTable.SetWidths(new[] { 50, 50 });
  181.  
  182. //logo
  183. if (logoExists)
  184. {
  185. var logoFilePath = _pictureService.GetThumbLocalPath(logoPicture, 0, false);
  186. var cellLogo = new PdfPCell(Image.GetInstance(logoFilePath));
  187. cellLogo.Border = Rectangle.NO_BORDER;
  188. headerTable.AddCell(cellLogo);
  189. }
  190. //store info
  191. var cell = new PdfPCell();
  192. cell.Border = Rectangle.NO_BORDER;
  193. cell.AddElement(new Paragraph(String.Format(_localizationService.GetResource("PDFInvoice.Order#", lang.Id), order.Id), titleFont));
  194. var store = _storeService.GetStoreById(order.StoreId) ?? _storeContext.CurrentStore;
  195. var anchor = new Anchor(store.Url.Trim(new char[] { '/' }), font);
  196. anchor.Reference = store.Url;
  197. cell.AddElement(new Paragraph(anchor));
  198. cell.AddElement(new Paragraph(String.Format(_localizationService.GetResource("PDFInvoice.OrderDate", lang.Id), _dateTimeHelper.ConvertToUserTime(order.CreatedOnUtc, DateTimeKind.Utc).ToString("D", new CultureInfo(lang.LanguageCulture))), font));
  199. headerTable.AddCell(cell);
  200. doc.Add(headerTable);
  201.  
  202. #endregion
  203.  
  204. #region Addresses
  205.  
  206. var addressTable = new PdfPTable(2);
  207. addressTable.WidthPercentage = 100f;
  208. addressTable.SetWidths(new[] { 50, 50 });
  209.  
  210.  
  211. //shipping info
  212. if (order.ShippingStatus != ShippingStatus.ShippingNotRequired)
  213. {
  214. if (order.ShippingAddress == null)
  215. throw new NopException(string.Format("Shipping is required, but address is not available. Order ID = {0}", order.Id));
  216. cell = new PdfPCell();
  217. cell.Border = Rectangle.NO_BORDER;
  218.  
  219. cell.AddElement(new Paragraph(_localizationService.GetResource("PDFInvoice.ShippingInformation", lang.Id), titleFont));
  220. if (!String.IsNullOrEmpty(order.ShippingAddress.Company))
  221. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Company", lang.Id), order.ShippingAddress.Company), font));
  222. cell.AddElement(new Paragraph(" " + String.Format(order.ShippingAddress.FirstName + order.ShippingAddress.LastName), font));
  223. //if (_addressSettings.PhoneEnabled)
  224. // cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Phone", lang.Id), order.ShippingAddress.PhoneNumber), font));
  225. if (_addressSettings.FaxEnabled && !String.IsNullOrEmpty(order.ShippingAddress.FaxNumber))
  226. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Fax", lang.Id), order.ShippingAddress.FaxNumber), font));
  227. if (_addressSettings.StreetAddressEnabled)
  228. cell.AddElement(new Paragraph(" " + String.Format(order.ShippingAddress.Address1), font));
  229. if (_addressSettings.StreetAddress2Enabled && !String.IsNullOrEmpty(order.ShippingAddress.Address2))
  230. cell.AddElement(new Paragraph(" " + String.Format(order.ShippingAddress.Address2), font));
  231. if (_addressSettings.CityEnabled || _addressSettings.StateProvinceEnabled || _addressSettings.ZipPostalCodeEnabled)
  232. cell.AddElement(new Paragraph(" " + String.Format("{0}, {1}", order.ShippingAddress.City, order.ShippingAddress.StateProvince != null ? order.ShippingAddress.StateProvince.GetLocalized(x => x.Name, lang.Id) : ""), font));
  233. cell.AddElement(new Paragraph(" " + String.Format(order.ShippingAddress.ZipPostalCode), font));
  234. //if (_addressSettings.CountryEnabled && order.ShippingAddress.Country != null)
  235. // cell.AddElement(new Paragraph(" " + String.Format("{0}", order.ShippingAddress.Country != null ? order.ShippingAddress.Country.GetLocalized(x => x.Name, lang.Id) : ""), font));
  236. cell.AddElement(new Paragraph(" "));
  237. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.ShippingMethod", lang.Id), order.ShippingMethod), font));
  238. cell.AddElement(new Paragraph());
  239.  
  240. addressTable.AddCell(cell);
  241. }
  242. else
  243. {
  244. cell = new PdfPCell(new Phrase(" "));
  245. cell.Border = Rectangle.NO_BORDER;
  246. addressTable.AddCell(cell);
  247. }
  248.  
  249.  
  250.  
  251. cell = new PdfPCell();
  252. cell.Border = Rectangle.NO_BORDER;
  253. cell.AddElement(new Paragraph(_localizationService.GetResource("PDFInvoice.BillingInformation", lang.Id), titleFont));
  254.  
  255. if (_addressSettings.CompanyEnabled && !String.IsNullOrEmpty(order.BillingAddress.Company))
  256. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Company", lang.Id), order.BillingAddress.Company), font));
  257.  
  258. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Name", lang.Id), order.BillingAddress.FirstName + " " + order.BillingAddress.LastName), font));
  259. if (_addressSettings.PhoneEnabled)
  260. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Phone", lang.Id), order.BillingAddress.PhoneNumber), font));
  261. if (_addressSettings.FaxEnabled && !String.IsNullOrEmpty(order.BillingAddress.FaxNumber))
  262. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Fax", lang.Id), order.BillingAddress.FaxNumber), font));
  263. if (_addressSettings.StreetAddressEnabled)
  264. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Address", lang.Id), order.BillingAddress.Address1), font));
  265. if (_addressSettings.StreetAddress2Enabled && !String.IsNullOrEmpty(order.BillingAddress.Address2))
  266. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.Address2", lang.Id), order.BillingAddress.Address2), font));
  267. if (_addressSettings.CityEnabled || _addressSettings.StateProvinceEnabled || _addressSettings.ZipPostalCodeEnabled)
  268. cell.AddElement(new Paragraph(" " + String.Format("{0}, {1} {2}", order.BillingAddress.City, order.BillingAddress.StateProvince != null ? order.BillingAddress.StateProvince.GetLocalized(x => x.Name, lang.Id) : "", order.BillingAddress.ZipPostalCode), font));
  269. if (_addressSettings.CountryEnabled && order.BillingAddress.Country != null)
  270. cell.AddElement(new Paragraph(" " + String.Format("{0}", order.BillingAddress.Country != null ? order.BillingAddress.Country.GetLocalized(x => x.Name, lang.Id) : ""), font));
  271.  
  272. //VAT number
  273. if (!String.IsNullOrEmpty(order.VatNumber))
  274. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.VATNumber", lang.Id), order.VatNumber), font));
  275.  
  276. //payment method
  277. var paymentMethod = _paymentService.LoadPaymentMethodBySystemName(order.PaymentMethodSystemName);
  278. string paymentMethodStr = paymentMethod != null ? paymentMethod.GetLocalizedFriendlyName(_localizationService, lang.Id) : order.PaymentMethodSystemName;
  279. if (!String.IsNullOrEmpty(paymentMethodStr))
  280. {
  281. cell.AddElement(new Paragraph(" "));
  282. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.PaymentMethod", lang.Id), paymentMethodStr), font));
  283. cell.AddElement(new Paragraph());
  284. }
  285.  
  286. //purchase order number (we have to find a better to inject this information because it's related to a certain plugin)
  287. if (paymentMethod != null && paymentMethod.PluginDescriptor.SystemName.Equals("Payments.PurchaseOrder", StringComparison.InvariantCultureIgnoreCase))
  288. {
  289. cell.AddElement(new Paragraph(" "));
  290. cell.AddElement(new Paragraph(" " + String.Format(_localizationService.GetResource("PDFInvoice.PurchaseOrderNumber", lang.Id), order.PurchaseOrderNumber), font));
  291. cell.AddElement(new Paragraph());
  292. }
  293.  
  294. addressTable.AddCell(cell);
  295.  
  296.  
  297.  
  298. doc.Add(addressTable);
  299. doc.Add(new Paragraph(" "));
  300.  
  301. #endregion
  302.  
  303. doc.Add(new Paragraph("Dear" + " " + String.Format(order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName), font));
  304. doc.Add(new Paragraph(" "));
  305. doc.Add(new Paragraph("Thank you for purchasing your ticket online.", font));
  306. doc.Add(new Paragraph("We greatly value your custom and we hope that you find the online ticket service both", font));
  307. doc.Add(new Paragraph("convenient and helpful.", font));
  308. doc.Add(new Paragraph(" "));
  309. doc.Add(new Paragraph("We wish you a pleasant time travelling around Blackpool and the The Fylde Coast.", font));
  310. doc.Add(new Paragraph(" "));
  311. doc.Add(new Paragraph("Regards", font));
  312. doc.Add(new Paragraph(" "));
  313. doc.Add(new Paragraph("Blackpool Transport", font));
  314. doc.Add(new Paragraph(" "));
  315.  
  316.  
  317.  
  318.  
  319. #region Products
  320. //products
  321. doc.Add(new Paragraph(_localizationService.GetResource("PDFInvoice.Product(s)", lang.Id), titleFont));
  322. doc.Add(new Paragraph(" "));
  323.  
  324.  
  325. var orderProductVariants = _orderService.GetAllOrderProductVariants(order.Id, null, null, null, null, null, null);
  326.  
  327. var productsTable = new PdfPTable(_catalogSettings.ShowProductSku ? 5 : 5);
  328. productsTable.WidthPercentage = 100f;
  329. productsTable.SetWidths(_catalogSettings.ShowProductSku ? new[] { 40, 15, 15, 15, 15 } : new[] { 40, 20, 20, 20, 20 });
  330.  
  331. //product name
  332. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductName", lang.Id), font));
  333. cell.BackgroundColor = BaseColor.YELLOW;
  334. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  335. productsTable.AddCell(cell);
  336.  
  337. //SKU
  338. if (_catalogSettings.ShowProductSku)
  339. {
  340. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.SKU", lang.Id), font));
  341. cell.BackgroundColor = BaseColor.YELLOW;
  342. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  343. productsTable.AddCell(cell);
  344. }
  345.  
  346. //Doc ID
  347.  
  348. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.DocID", lang.Id), font));
  349. cell.BackgroundColor = BaseColor.YELLOW;
  350. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  351. productsTable.AddCell(cell);
  352.  
  353.  
  354. //price
  355. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductPrice", lang.Id), font));
  356. cell.BackgroundColor = BaseColor.YELLOW;
  357. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  358. productsTable.AddCell(cell);
  359.  
  360. //qty
  361. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductQuantity", lang.Id), font));
  362. cell.BackgroundColor = BaseColor.YELLOW;
  363. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  364. productsTable.AddCell(cell);
  365.  
  366. //total
  367. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.ProductTotal", lang.Id), font));
  368. cell.BackgroundColor = BaseColor.YELLOW;
  369. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  370. productsTable.AddCell(cell);
  371.  
  372. for (int i = 0; i < orderProductVariants.Count; i++)
  373. {
  374. var orderProductVariant = orderProductVariants[i];
  375. var pv = orderProductVariant.ProductVariant;
  376.  
  377. //product name
  378. string name = "";
  379. if (!String.IsNullOrEmpty(pv.GetLocalized(x => x.Name, lang.Id)))
  380. name = string.Format("{0} ({1})", pv.Product.GetLocalized(x => x.Name, lang.Id), pv.GetLocalized(x => x.Name, lang.Id));
  381. else
  382. name = pv.Product.GetLocalized(x => x.Name, lang.Id);
  383. cell = new PdfPCell(new Phrase(name, font));
  384. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  385. //var attributesParagraph = new Paragraph(HtmlHelper.ConvertHtmlToPlainText(orderProductVariant.AttributeDescription, true, true), attributesFont);
  386. //cell.AddElement(attributesParagraph);
  387. productsTable.AddCell(cell);
  388.  
  389.  
  390. //DOCID
  391.  
  392. var document = System.Xml.Linq.XDocument.Parse(item.AttributesXml);
  393.  
  394. var attribute = document.Element("Attributes").Elements("ProductVariantAttribute").Where(e => e.Attribute("ID") != null && e.Attribute("ID").Value == attributeId.ToString()).FirstOrDefault();
  395.  
  396. if (attribute != null)
  397. {
  398. var value = attribute.Descendants("Value").FirstOrDefault();
  399.  
  400. if (value != null && value.Value.ToLower() == documentId.ToLower())
  401. {
  402. return item;
  403. }
  404. }
  405.  
  406. cell = new PdfPCell(new Phrase(documentId?? String.Empty, font));
  407. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  408. productsTable.AddCell(cell);
  409.  
  410.  
  411. //String docID = String.Empty;
  412. //if (values.Count > 0)
  413. //{
  414. // docID = values.First();
  415. //}
  416.  
  417.  
  418. //cell = new PdfPCell(new Phrase(docID));
  419. //cell.HorizontalAlignment = Element.ALIGN_CENTER;
  420. //productsTable.AddCell(cell);
  421.  
  422.  
  423.  
  424. // var item = new OrderProductVariant ();
  425.  
  426. //int DocumentId = 6;
  427.  
  428. // IList<string> values = _productAttributeParser.ParseValues(item.AttributesXml, DocumentId);
  429.  
  430. // String docID = String.Empty;
  431. // if (values.Count > 0)
  432. // {
  433. // docID = values.FirstOrDefault();
  434. // }
  435. // cell = new PdfPCell(new Phrase(docID));
  436. // cell.HorizontalAlignment = Element.ALIGN_CENTER;
  437. // productsTable.AddCell(cell);
  438.  
  439. //SKU
  440. if (_catalogSettings.ShowProductSku)
  441. {
  442. var sku = pv.FormatSku(orderProductVariant.AttributesXml, _productAttributeParser);
  443. cell = new PdfPCell(new Phrase(sku ?? String.Empty, font));
  444. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  445. productsTable.AddCell(cell);
  446. }
  447.  
  448. //price
  449. string unitPrice = string.Empty;
  450. switch (order.CustomerTaxDisplayType)
  451. {
  452. case TaxDisplayType.ExcludingTax:
  453. {
  454. var opvUnitPriceExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(orderProductVariant.UnitPriceExclTax, order.CurrencyRate);
  455. unitPrice = _priceFormatter.FormatPrice(opvUnitPriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
  456. }
  457. break;
  458. case TaxDisplayType.IncludingTax:
  459. {
  460. var opvUnitPriceInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(orderProductVariant.UnitPriceInclTax, order.CurrencyRate);
  461. unitPrice = _priceFormatter.FormatPrice(opvUnitPriceInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
  462. }
  463. break;
  464. }
  465. cell = new PdfPCell(new Phrase(unitPrice, font));
  466. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  467. productsTable.AddCell(cell);
  468.  
  469. //qty
  470. cell = new PdfPCell(new Phrase(orderProductVariant.Quantity.ToString(), font));
  471. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  472. productsTable.AddCell(cell);
  473.  
  474. //total
  475. string subTotal = string.Empty;
  476. switch (order.CustomerTaxDisplayType)
  477. {
  478. case TaxDisplayType.ExcludingTax:
  479. {
  480. var opvPriceExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(orderProductVariant.PriceExclTax, order.CurrencyRate);
  481. subTotal = _priceFormatter.FormatPrice(opvPriceExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
  482. }
  483. break;
  484. case TaxDisplayType.IncludingTax:
  485. {
  486. var opvPriceInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(orderProductVariant.PriceInclTax, order.CurrencyRate);
  487. subTotal = _priceFormatter.FormatPrice(opvPriceInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
  488. }
  489. break;
  490. }
  491. cell = new PdfPCell(new Phrase(subTotal, font));
  492. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  493. productsTable.AddCell(cell);
  494. }
  495. doc.Add(productsTable);
  496.  
  497. #endregion
  498.  
  499. #region Checkout attributes
  500.  
  501. if (!String.IsNullOrEmpty(order.CheckoutAttributeDescription))
  502. {
  503. doc.Add(new Paragraph(" "));
  504. string attributes = HtmlHelper.ConvertHtmlToPlainText(order.CheckoutAttributeDescription, true, true);
  505. var pCheckoutAttributes = new Paragraph(attributes, font);
  506. pCheckoutAttributes.Alignment = Element.ALIGN_RIGHT;
  507. doc.Add(pCheckoutAttributes);
  508. doc.Add(new Paragraph(" "));
  509. }
  510.  
  511. #endregion
  512.  
  513. #region Totals
  514.  
  515. //subtotal
  516. doc.Add(new Paragraph(" "));
  517. switch (order.CustomerTaxDisplayType)
  518. {
  519. case TaxDisplayType.ExcludingTax:
  520. {
  521. var orderSubtotalExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubtotalExclTax, order.CurrencyRate);
  522. string orderSubtotalExclTaxStr = _priceFormatter.FormatPrice(orderSubtotalExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
  523.  
  524. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Sub-Total", lang.Id), orderSubtotalExclTaxStr), font);
  525. p.Alignment = Element.ALIGN_RIGHT;
  526. doc.Add(p);
  527. }
  528. break;
  529. case TaxDisplayType.IncludingTax:
  530. {
  531. var orderSubtotalInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubtotalInclTax, order.CurrencyRate);
  532. string orderSubtotalInclTaxStr = _priceFormatter.FormatPrice(orderSubtotalInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
  533.  
  534. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Sub-Total", lang.Id), orderSubtotalInclTaxStr), font);
  535. p.Alignment = Element.ALIGN_RIGHT;
  536. doc.Add(p);
  537. }
  538. break;
  539. }
  540. //discount (applied to order subtotal)
  541. if (order.OrderSubTotalDiscountExclTax > decimal.Zero)
  542. {
  543. switch (order.CustomerTaxDisplayType)
  544. {
  545. case TaxDisplayType.ExcludingTax:
  546. {
  547. var orderSubTotalDiscountExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubTotalDiscountExclTax, order.CurrencyRate);
  548. string orderSubTotalDiscountInCustomerCurrencyStr = _priceFormatter.FormatPrice(-orderSubTotalDiscountExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
  549.  
  550. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Discount", lang.Id), orderSubTotalDiscountInCustomerCurrencyStr), font);
  551. p.Alignment = Element.ALIGN_RIGHT;
  552. doc.Add(p);
  553. }
  554. break;
  555. case TaxDisplayType.IncludingTax:
  556. {
  557. var orderSubTotalDiscountInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderSubTotalDiscountInclTax, order.CurrencyRate);
  558. string orderSubTotalDiscountInCustomerCurrencyStr = _priceFormatter.FormatPrice(-orderSubTotalDiscountInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
  559.  
  560. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Discount", lang.Id), orderSubTotalDiscountInCustomerCurrencyStr), font);
  561. p.Alignment = Element.ALIGN_RIGHT;
  562. doc.Add(p);
  563. }
  564. break;
  565. }
  566. }
  567.  
  568. //shipping
  569. if (order.ShippingStatus != ShippingStatus.ShippingNotRequired)
  570. {
  571. switch (order.CustomerTaxDisplayType)
  572. {
  573. case TaxDisplayType.ExcludingTax:
  574. {
  575. var orderShippingExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderShippingExclTax, order.CurrencyRate);
  576. string orderShippingExclTaxStr = _priceFormatter.FormatShippingPrice(orderShippingExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
  577.  
  578. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Shipping", lang.Id), orderShippingExclTaxStr), font);
  579. p.Alignment = Element.ALIGN_RIGHT;
  580. doc.Add(p);
  581. }
  582. break;
  583. case TaxDisplayType.IncludingTax:
  584. {
  585. var orderShippingInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderShippingInclTax, order.CurrencyRate);
  586. string orderShippingInclTaxStr = _priceFormatter.FormatShippingPrice(orderShippingInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
  587.  
  588. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Shipping", lang.Id), orderShippingInclTaxStr), font);
  589. p.Alignment = Element.ALIGN_RIGHT;
  590. doc.Add(p);
  591. }
  592. break;
  593. }
  594. }
  595.  
  596. //payment fee
  597. if (order.PaymentMethodAdditionalFeeExclTax > decimal.Zero)
  598. {
  599. switch (order.CustomerTaxDisplayType)
  600. {
  601. case TaxDisplayType.ExcludingTax:
  602. {
  603. var paymentMethodAdditionalFeeExclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.PaymentMethodAdditionalFeeExclTax, order.CurrencyRate);
  604. string paymentMethodAdditionalFeeExclTaxStr = _priceFormatter.FormatPaymentMethodAdditionalFee(paymentMethodAdditionalFeeExclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, false);
  605.  
  606. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.PaymentMethodAdditionalFee", lang.Id), paymentMethodAdditionalFeeExclTaxStr), font);
  607. p.Alignment = Element.ALIGN_RIGHT;
  608. doc.Add(p);
  609. }
  610. break;
  611. case TaxDisplayType.IncludingTax:
  612. {
  613. var paymentMethodAdditionalFeeInclTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.PaymentMethodAdditionalFeeInclTax, order.CurrencyRate);
  614. string paymentMethodAdditionalFeeInclTaxStr = _priceFormatter.FormatPaymentMethodAdditionalFee(paymentMethodAdditionalFeeInclTaxInCustomerCurrency, true, order.CustomerCurrencyCode, lang, true);
  615.  
  616. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.PaymentMethodAdditionalFee", lang.Id), paymentMethodAdditionalFeeInclTaxStr), font);
  617. p.Alignment = Element.ALIGN_RIGHT;
  618. doc.Add(p);
  619. }
  620. break;
  621. }
  622. }
  623.  
  624. //tax
  625. string taxStr = string.Empty;
  626. var taxRates = new SortedDictionary<decimal, decimal>();
  627. bool displayTax = true;
  628. bool displayTaxRates = true;
  629. if (_taxSettings.HideTaxInOrderSummary && order.CustomerTaxDisplayType == TaxDisplayType.IncludingTax)
  630. {
  631. displayTax = false;
  632. }
  633. else
  634. {
  635. if (order.OrderTax == 0 && _taxSettings.HideZeroTax)
  636. {
  637. displayTax = false;
  638. displayTaxRates = false;
  639. }
  640. else
  641. {
  642. taxRates = order.TaxRatesDictionary;
  643.  
  644. displayTaxRates = _taxSettings.DisplayTaxRates && taxRates.Count > 0;
  645. displayTax = !displayTaxRates;
  646.  
  647. var orderTaxInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderTax, order.CurrencyRate);
  648. taxStr = _priceFormatter.FormatPrice(orderTaxInCustomerCurrency, true, order.CustomerCurrencyCode, false, lang);
  649. }
  650. }
  651. if (displayTax)
  652. {
  653. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Tax", lang.Id), taxStr), font);
  654. p.Alignment = Element.ALIGN_RIGHT;
  655. doc.Add(p);
  656. }
  657. if (displayTaxRates)
  658. {
  659. foreach (var item in taxRates)
  660. {
  661. string taxRate = String.Format(_localizationService.GetResource("PDFInvoice.TaxRate", lang.Id), _priceFormatter.FormatTaxRate(item.Key));
  662. string taxValue = _priceFormatter.FormatPrice(_currencyService.ConvertCurrency(item.Value, order.CurrencyRate), true, order.CustomerCurrencyCode, false, lang);
  663.  
  664. var p = new Paragraph(String.Format("{0} {1}", taxRate, taxValue), font);
  665. p.Alignment = Element.ALIGN_RIGHT;
  666. doc.Add(p);
  667. }
  668. }
  669.  
  670. //discount (applied to order total)
  671. if (order.OrderDiscount > decimal.Zero)
  672. {
  673. var orderDiscountInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderDiscount, order.CurrencyRate);
  674. string orderDiscountInCustomerCurrencyStr = _priceFormatter.FormatPrice(-orderDiscountInCustomerCurrency, true, order.CustomerCurrencyCode, false, lang);
  675.  
  676. var p = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.Discount", lang.Id), orderDiscountInCustomerCurrencyStr), font);
  677. p.Alignment = Element.ALIGN_RIGHT;
  678. doc.Add(p);
  679. }
  680.  
  681. //gift cards
  682. foreach (var gcuh in order.GiftCardUsageHistory)
  683. {
  684. string gcTitle = string.Format(_localizationService.GetResource("PDFInvoice.GiftCardInfo", lang.Id), gcuh.GiftCard.GiftCardCouponCode);
  685. string gcAmountStr = _priceFormatter.FormatPrice(-(_currencyService.ConvertCurrency(gcuh.UsedValue, order.CurrencyRate)), true, order.CustomerCurrencyCode, false, lang);
  686.  
  687. var p = new Paragraph(String.Format("{0} {1}", gcTitle, gcAmountStr), font);
  688. p.Alignment = Element.ALIGN_RIGHT;
  689. doc.Add(p);
  690. }
  691.  
  692. //reward points
  693. if (order.RedeemedRewardPointsEntry != null)
  694. {
  695. string rpTitle = string.Format(_localizationService.GetResource("PDFInvoice.RewardPoints", lang.Id), -order.RedeemedRewardPointsEntry.Points);
  696. string rpAmount = _priceFormatter.FormatPrice(-(_currencyService.ConvertCurrency(order.RedeemedRewardPointsEntry.UsedAmount, order.CurrencyRate)), true, order.CustomerCurrencyCode, false, lang);
  697.  
  698. var p = new Paragraph(String.Format("{0} {1}", rpTitle, rpAmount), font);
  699. p.Alignment = Element.ALIGN_RIGHT;
  700. doc.Add(p);
  701. }
  702.  
  703. //order total
  704. var orderTotalInCustomerCurrency = _currencyService.ConvertCurrency(order.OrderTotal, order.CurrencyRate);
  705. string orderTotalStr = _priceFormatter.FormatPrice(orderTotalInCustomerCurrency, true, order.CustomerCurrencyCode, false, lang);
  706.  
  707.  
  708. var pTotal = new Paragraph(String.Format("{0} {1}", _localizationService.GetResource("PDFInvoice.OrderTotal", lang.Id), orderTotalStr), titleFont);
  709. pTotal.Alignment = Element.ALIGN_RIGHT;
  710. doc.Add(pTotal);
  711.  
  712.  
  713. doc.Add(new Paragraph("IMPORTANT INFORMATION - PLEASE READ THE FOLLOWING:", titleFont));
  714.  
  715.  
  716. doc.Add(new Paragraph(" "));
  717.  
  718. doc.Add(new Paragraph("Your online order has been processed and dispatched by our business partner Orion Security Print Ltd.", font));
  719. doc.Add(new Paragraph("As such, please be aware that merchant information on your bank statement, credit card statement", font));
  720. doc.Add(new Paragraph("or PayPal account will appear as 'Orion Security Print Ltd'.", font));
  721. doc.Add(new Paragraph(" "));
  722. doc.Add(new Paragraph("If you have any further questions please dont hesitate to contact us via email;", font));
  723. doc.Add(new Paragraph("tickets@blackpooltransport.com", font));
  724.  
  725. cell = new PdfPCell(new Phrase("Blackpool Transport Ltd", font));
  726. cell.AddElement(new Paragraph("Rigby Road, Blackpool, FY1 5DD, England, Tel 01253 473001, Company Registered number 2003020"));
  727. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  728.  
  729.  
  730. #endregion
  731.  
  732. #region Order notes
  733.  
  734. if (_pdfSettings.RenderOrderNotes)
  735. {
  736. var orderNotes = order.OrderNotes
  737. .Where(on => on.DisplayToCustomer)
  738. .OrderByDescending(on => on.CreatedOnUtc)
  739. .ToList();
  740. if (orderNotes.Count > 0)
  741. {
  742. doc.Add(new Paragraph(_localizationService.GetResource("PDFInvoice.OrderNotes", lang.Id), titleFont));
  743.  
  744. doc.Add(new Paragraph(" "));
  745.  
  746. var notesTable = new PdfPTable(2);
  747. notesTable.WidthPercentage = 100f;
  748. notesTable.SetWidths(new[] { 30, 70 });
  749.  
  750. //created on
  751. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.OrderNotes.CreatedOn", lang.Id), font));
  752. cell.BackgroundColor = BaseColor.YELLOW;
  753. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  754. notesTable.AddCell(cell);
  755.  
  756. //note
  757. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFInvoice.OrderNotes.Note", lang.Id), font));
  758. cell.BackgroundColor = BaseColor.YELLOW;
  759. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  760. notesTable.AddCell(cell);
  761.  
  762. foreach (var orderNote in orderNotes)
  763. {
  764. cell = new PdfPCell();
  765. cell.AddElement(new Paragraph(_dateTimeHelper.ConvertToUserTime(orderNote.CreatedOnUtc, DateTimeKind.Utc).ToString(), font));
  766. cell.HorizontalAlignment = Element.ALIGN_LEFT;
  767. notesTable.AddCell(cell);
  768.  
  769. cell = new PdfPCell();
  770. cell.AddElement(new Paragraph(HtmlHelper.ConvertHtmlToPlainText(orderNote.FormatOrderNoteText(), true, true), font));
  771. cell.HorizontalAlignment = Element.ALIGN_LEFT;
  772. notesTable.AddCell(cell);
  773. }
  774. doc.Add(notesTable);
  775.  
  776.  
  777. }
  778. }
  779.  
  780. #endregion
  781.  
  782. ordNum++;
  783. if (ordNum < ordCount)
  784. {
  785. doc.NewPage();
  786. }
  787. }
  788. doc.Close();
  789. }
  790.  
  791. /// <summary>
  792. /// Print packaging slips to PDF
  793. /// </summary>
  794. /// <param name="stream">Stream</param>
  795. /// <param name="shipments">Shipments</param>
  796. /// <param name="languageId">Language identifier; 0 to use a language used when placing an order</param>
  797. public virtual void PrintPackagingSlipsToPdf(Stream stream, IList<Shipment> shipments, int languageId = 0)
  798. {
  799. if (stream == null)
  800. throw new ArgumentNullException("stream");
  801.  
  802. if (shipments == null)
  803. throw new ArgumentNullException("shipments");
  804.  
  805. var lang = _languageService.GetLanguageById(languageId);
  806. if (lang == null)
  807. throw new ArgumentException(string.Format("Cannot load language. ID={0}", languageId));
  808.  
  809. var pageSize = PageSize.A4;
  810.  
  811. if (_pdfSettings.LetterPageSizeEnabled)
  812. {
  813. pageSize = PageSize.LETTER;
  814. }
  815.  
  816. var doc = new Document(pageSize);
  817. PdfWriter.GetInstance(doc, stream);
  818. doc.Open();
  819.  
  820. //fonts
  821. var titleFont = GetFont();
  822. titleFont.SetStyle(Font.BOLD);
  823. titleFont.Color = BaseColor.BLACK;
  824. var font = GetFont();
  825. var attributesFont = GetFont();
  826. attributesFont.SetStyle(Font.ITALIC);
  827.  
  828. int shipmentCount = shipments.Count;
  829. int shipmentNum = 0;
  830.  
  831. foreach (var shipment in shipments)
  832. {
  833. var order = shipment.Order;
  834.  
  835. if (languageId == 0)
  836. {
  837. lang = _languageService.GetLanguageById(order.CustomerLanguageId);
  838. if (lang == null || !lang.Published)
  839. lang = _workContext.WorkingLanguage;
  840. }
  841.  
  842.  
  843. if (order.ShippingAddress != null)
  844. {
  845. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Shipment", lang.Id), shipment.Id), titleFont));
  846. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Order", lang.Id), order.Id), titleFont));
  847.  
  848. if (_addressSettings.CompanyEnabled && !String.IsNullOrEmpty(order.ShippingAddress.Company))
  849. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Company", lang.Id), order.ShippingAddress.Company), font));
  850.  
  851. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Name", lang.Id), order.ShippingAddress.FirstName + " " + order.ShippingAddress.LastName), font));
  852. if (_addressSettings.PhoneEnabled)
  853. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Phone", lang.Id), order.ShippingAddress.PhoneNumber), font));
  854. if (_addressSettings.StreetAddressEnabled)
  855. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Address", lang.Id), order.ShippingAddress.Address1), font));
  856.  
  857. if (_addressSettings.StreetAddress2Enabled && !String.IsNullOrEmpty(order.ShippingAddress.Address2))
  858. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.Address2", lang.Id), order.ShippingAddress.Address2), font));
  859.  
  860. if (_addressSettings.CityEnabled || _addressSettings.StateProvinceEnabled || _addressSettings.ZipPostalCodeEnabled)
  861. doc.Add(new Paragraph(String.Format("{0}, {1} {2}", order.ShippingAddress.City, order.ShippingAddress.StateProvince != null ? order.ShippingAddress.StateProvince.GetLocalized(x => x.Name, lang.Id) : "", order.ShippingAddress.ZipPostalCode), font));
  862.  
  863. if (_addressSettings.CountryEnabled && order.ShippingAddress.Country != null)
  864. doc.Add(new Paragraph(String.Format("{0}", order.ShippingAddress.Country != null ? order.ShippingAddress.Country.GetLocalized(x => x.Name, lang.Id) : ""), font));
  865.  
  866. doc.Add(new Paragraph(" "));
  867.  
  868. doc.Add(new Paragraph(String.Format(_localizationService.GetResource("PDFPackagingSlip.ShippingMethod", lang.Id), order.ShippingMethod), font));
  869. doc.Add(new Paragraph(" "));
  870.  
  871. var productsTable = new PdfPTable(3);
  872. productsTable.WidthPercentage = 100f;
  873. productsTable.SetWidths(new[] { 60, 20, 20 });
  874.  
  875. //product name
  876. var cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.ProductName", lang.Id), font));
  877. cell.BackgroundColor = BaseColor.YELLOW;
  878. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  879. productsTable.AddCell(cell);
  880.  
  881. //SKU
  882. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.SKU", lang.Id), font));
  883. cell.BackgroundColor = BaseColor.YELLOW;
  884. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  885. productsTable.AddCell(cell);
  886.  
  887. //qty
  888. cell = new PdfPCell(new Phrase(_localizationService.GetResource("PDFPackagingSlip.QTY", lang.Id), font));
  889. cell.BackgroundColor = BaseColor.YELLOW;
  890. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  891. productsTable.AddCell(cell);
  892.  
  893. foreach (var sopv in shipment.ShipmentOrderProductVariants)
  894. {
  895. //product name
  896. var opv = _orderService.GetOrderProductVariantById(sopv.OrderProductVariantId);
  897. if (opv == null)
  898. continue;
  899.  
  900. var pv = opv.ProductVariant;
  901. string name = "";
  902. if (!String.IsNullOrEmpty(pv.GetLocalized(x => x.Name, lang.Id)))
  903. name = string.Format("{0} ({1})", pv.Product.GetLocalized(x => x.Name, lang.Id), pv.GetLocalized(x => x.Name, lang.Id));
  904. else
  905. name = pv.Product.GetLocalized(x => x.Name, lang.Id);
  906. cell = new PdfPCell();
  907. cell.AddElement(new Paragraph(name, font));
  908. cell.HorizontalAlignment = Element.ALIGN_LEFT;
  909. var attributesParagraph = new Paragraph(HtmlHelper.ConvertHtmlToPlainText(opv.AttributeDescription, true, true), attributesFont);
  910. cell.AddElement(attributesParagraph);
  911. productsTable.AddCell(cell);
  912.  
  913. //SKU
  914. var sku = pv.FormatSku(opv.AttributesXml, _productAttributeParser);
  915. cell = new PdfPCell(new Phrase(sku ?? String.Empty, font));
  916. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  917. productsTable.AddCell(cell);
  918.  
  919. //qty
  920. cell = new PdfPCell(new Phrase(sopv.Quantity.ToString(), font));
  921. cell.HorizontalAlignment = Element.ALIGN_CENTER;
  922. productsTable.AddCell(cell);
  923. }
  924. doc.Add(productsTable);
  925. }
  926.  
  927. shipmentNum++;
  928. if (shipmentNum < shipmentCount)
  929. {
  930. doc.NewPage();
  931. }
  932. }
  933.  
  934.  
  935. doc.Close();
  936. }
  937.  
  938. /// <summary>
  939. /// Print product collection to PDF
  940. /// </summary>
  941. /// <param name="stream">Stream</param>
  942. /// <param name="products">Products</param>
  943. public virtual void PrintProductsToPdf(Stream stream, IList<Product> products)
  944. {
  945. if (stream == null)
  946. throw new ArgumentNullException("stream");
  947.  
  948. if (products == null)
  949. throw new ArgumentNullException("products");
  950.  
  951. var lang = _workContext.WorkingLanguage;
  952.  
  953. var pageSize = PageSize.A4;
  954.  
  955. if (_pdfSettings.LetterPageSizeEnabled)
  956. {
  957. pageSize = PageSize.LETTER;
  958. }
  959.  
  960. var doc = new Document(pageSize);
  961. PdfWriter.GetInstance(doc, stream);
  962. doc.Open();
  963.  
  964. //fonts
  965. var titleFont = GetFont();
  966. titleFont.SetStyle(Font.BOLD);
  967. titleFont.Color = BaseColor.BLACK;
  968. var font = GetFont();
  969.  
  970. int productNumber = 1;
  971. int prodCount = products.Count;
  972.  
  973. foreach (var product in products)
  974. {
  975. string productName = product.GetLocalized(x => x.Name, lang.Id);
  976. string productFullDescription = product.GetLocalized(x => x.FullDescription, lang.Id);
  977.  
  978. doc.Add(new Paragraph(String.Format("{0}. {1}", productNumber, productName), titleFont));
  979. doc.Add(new Paragraph(" "));
  980. doc.Add(new Paragraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(productFullDescription)), font));
  981. doc.Add(new Paragraph(" "));
  982.  
  983. var pictures = _pictureService.GetPicturesByProductId(product.Id);
  984. if (pictures.Count > 0)
  985. {
  986. var table = new PdfPTable(2);
  987. table.WidthPercentage = 100f;
  988.  
  989. for (int i = 0; i < pictures.Count; i++)
  990. {
  991. var pic = pictures[i];
  992. if (pic != null)
  993. {
  994. var picBinary = _pictureService.LoadPictureBinary(pic);
  995. if (picBinary != null && picBinary.Length > 0)
  996. {
  997. var pictureLocalPath = _pictureService.GetThumbLocalPath(pic, 200, false);
  998. var cell = new PdfPCell(Image.GetInstance(pictureLocalPath));
  999. cell.HorizontalAlignment = Element.ALIGN_LEFT;
  1000. cell.Border = Rectangle.NO_BORDER;
  1001. table.AddCell(cell);
  1002. }
  1003. }
  1004. }
  1005.  
  1006. if (pictures.Count % 2 > 0)
  1007. {
  1008. var cell = new PdfPCell(new Phrase(" "));
  1009. cell.Border = Rectangle.NO_BORDER;
  1010. table.AddCell(cell);
  1011. }
  1012.  
  1013. doc.Add(table);
  1014. doc.Add(new Paragraph(" "));
  1015. }
  1016.  
  1017. int pvNum = 1;
  1018.  
  1019. foreach (var productVariant in _productService.GetProductVariantsByProductId(product.Id, true))
  1020. {
  1021. string pvName = String.IsNullOrEmpty(productVariant.GetLocalized(x => x.Name, lang.Id)) ? _localizationService.GetResource("PDFProductCatalog.UnnamedProductVariant", lang.Id) : productVariant.GetLocalized(x => x.Name, lang.Id);
  1022.  
  1023. doc.Add(new Paragraph(String.Format("{0}.{1}. {2}", productNumber, pvNum, pvName), font));
  1024. doc.Add(new Paragraph(" "));
  1025.  
  1026. string productVariantDescription = productVariant.GetLocalized(x => x.Description, lang.Id);
  1027. if (!String.IsNullOrEmpty(productVariantDescription))
  1028. {
  1029. doc.Add(new Paragraph(HtmlHelper.StripTags(HtmlHelper.ConvertHtmlToPlainText(productVariantDescription)), font));
  1030. doc.Add(new Paragraph(" "));
  1031. }
  1032.  
  1033. var pic = _pictureService.GetPictureById(productVariant.PictureId);
  1034. if (pic != null)
  1035. {
  1036. var picBinary = _pictureService.LoadPictureBinary(pic);
  1037. if (picBinary != null && picBinary.Length > 0)
  1038. {
  1039. var pictureLocalPath = _pictureService.GetThumbLocalPath(pic, 200, false);
  1040. doc.Add(Image.GetInstance(pictureLocalPath));
  1041. }
  1042. }
  1043.  
  1044. doc.Add(new Paragraph(String.Format("{0}: {1} {2}", _localizationService.GetResource("PDFProductCatalog.Price", lang.Id), productVariant.Price.ToString("0.00"), _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode), font));
  1045. doc.Add(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.SKU", lang.Id), productVariant.Sku), font));
  1046.  
  1047. if (productVariant.IsShipEnabled && productVariant.Weight > Decimal.Zero)
  1048. doc.Add(new Paragraph(String.Format("{0}: {1} {2}", _localizationService.GetResource("PDFProductCatalog.Weight", lang.Id), productVariant.Weight.ToString("0.00"), _measureService.GetMeasureWeightById(_measureSettings.BaseWeightId).Name), font));
  1049.  
  1050. if (productVariant.ManageInventoryMethod == ManageInventoryMethod.ManageStock)
  1051. doc.Add(new Paragraph(String.Format("{0}: {1}", _localizationService.GetResource("PDFProductCatalog.StockQuantity", lang.Id), productVariant.StockQuantity), font));
  1052.  
  1053. doc.Add(new Paragraph(" "));
  1054.  
  1055. pvNum++;
  1056. }
  1057.  
  1058. productNumber++;
  1059.  
  1060. if (productNumber <= prodCount)
  1061. {
  1062. doc.NewPage();
  1063. }
  1064. }
  1065.  
  1066. doc.Close();
  1067. }
  1068.  
  1069. #endregion
  1070. }
  1071. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(5,20): error CS0234: The type or namespace name `Stores' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(6,7): error CS0246: The type or namespace name `iTextSharp' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(7,7): error CS0246: The type or namespace name `iTextSharp' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(8,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(9,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(10,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(11,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(12,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(13,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(14,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(15,11): error CS0234: The type or namespace name `Core' does not exist in the namespace `Nop'. Are you missing an assembly reference?
prog.cs(16,20): error CS0234: The type or namespace name `Catalog' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(17,20): error CS0234: The type or namespace name `Directory' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(18,20): error CS0234: The type or namespace name `Helpers' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(19,20): error CS0234: The type or namespace name `Localization' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(20,20): error CS0234: The type or namespace name `Media' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(21,20): error CS0234: The type or namespace name `Orders' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(22,20): error CS0234: The type or namespace name `Payments' does not exist in the namespace `Nop.Services'. Are you missing an assembly reference?
prog.cs(34,26): error CS0246: The type or namespace name `ILocalizationService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(35,26): error CS0246: The type or namespace name `ILanguageService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(36,26): error CS0246: The type or namespace name `IWorkContext' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(37,26): error CS0246: The type or namespace name `IOrderService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(38,26): error CS0246: The type or namespace name `IPaymentService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(39,26): error CS0246: The type or namespace name `IDateTimeHelper' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(40,26): error CS0246: The type or namespace name `IPriceFormatter' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(41,26): error CS0246: The type or namespace name `ICurrencyService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(42,26): error CS0246: The type or namespace name `IMeasureService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(43,26): error CS0246: The type or namespace name `IPictureService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(44,26): error CS0246: The type or namespace name `IProductService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(45,26): error CS0246: The type or namespace name `IProductAttributeParser' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(46,26): error CS0246: The type or namespace name `IStoreService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(47,26): error CS0246: The type or namespace name `IStoreContext' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(48,26): error CS0246: The type or namespace name `IWebHelper' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(50,26): error CS0246: The type or namespace name `CatalogSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(51,26): error CS0246: The type or namespace name `CurrencySettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(52,26): error CS0246: The type or namespace name `MeasureSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(53,26): error CS0246: The type or namespace name `PdfSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(54,26): error CS0246: The type or namespace name `TaxSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(55,26): error CS0246: The type or namespace name `AddressSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(61,27): error CS0246: The type or namespace name `ILocalizationService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(62,13): error CS0246: The type or namespace name `ILanguageService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(63,13): error CS0246: The type or namespace name `IWorkContext' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(64,13): error CS0246: The type or namespace name `IOrderService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(65,13): error CS0246: The type or namespace name `IPaymentService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(66,13): error CS0246: The type or namespace name `IDateTimeHelper' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(66,45): error CS0246: The type or namespace name `IPriceFormatter' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(67,13): error CS0246: The type or namespace name `ICurrencyService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(67,47): error CS0246: The type or namespace name `IMeasureService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(68,13): error CS0246: The type or namespace name `IPictureService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(68,45): error CS0246: The type or namespace name `IProductService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(69,13): error CS0246: The type or namespace name `IProductAttributeParser' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(69,61): error CS0246: The type or namespace name `IStoreService' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(70,13): error CS0246: The type or namespace name `IStoreContext' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(70,41): error CS0246: The type or namespace name `IWebHelper' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(71,13): error CS0246: The type or namespace name `CatalogSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(71,46): error CS0246: The type or namespace name `CurrencySettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(72,13): error CS0246: The type or namespace name `MeasureSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(72,46): error CS0246: The type or namespace name `PdfSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(72,71): error CS0246: The type or namespace name `TaxSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(73,13): error CS0246: The type or namespace name `AddressSettings' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(102,27): error CS0246: The type or namespace name `Font' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(124,67): error CS0246: The type or namespace name `Order' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(797,75): error CS0246: The type or namespace name `Shipment' could not be found. Are you missing a using directive or an assembly reference?
prog.cs(943,69): error CS0246: The type or namespace name `Product' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 64 error(s), 0 warnings
stdout
Standard output is empty