Developer forum

Forum » Templates » Create order as PDF and attach it to email notification

Create order as PDF and attach it to email notification

Jens Mouritzen
Jens Mouritzen
Reply

Hi guys
We would like to know, if there is a simple way to generate a PDF file of an order, and send it to the order notification recipient?
E.g. if the user makes an order, then the solution should send an email with the order attached.

We could simply just send the order in the email as html, but we would like to "lock" the text and other graphics in a pdf, due to specific print demands.

How can we achive this?


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Jens

There is no simple way to do that.

You can create a notification subscriber on the email sending of the order () and then create a PDF document using AbcPdf found in Dynamicweb - something like this:

using Dynamicweb.Core;
using WebSupergoo.ABCpdf9;

namespace Dynamicweb.Ecommerce.Examples.Notifications
  {
      [Dynamicweb.Extensibility.Notifications.Subscribe(Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMail)]
      public class EcomCartSendingConfirmationMailObserver : Dynamicweb.Extensibility.Notifications.NotificationSubscriber
      {
          public override void OnNotify(string notification, Dynamicweb.Extensibility.Notifications.NotificationArgs args)
          {
              Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMailArgs sendingConfirmationMailArgs = args as Dynamicweb.Ecommerce.Notifications.Ecommerce.Cart.SendingConfirmationMailArgs;

            //EXAMPLE with inline markup
            //Dynamicweb.Rendering.Template htmlForPdfTemplate = new Rendering.Template();
            //tmlForPdfTemplate.Html = "<html><body><h1>Receipt</h1>@SomeRazorCode and regular Dynamicweb template language</body></html>";

            //EXAMPLE with a razor template
            //Load a template - with full html, including html, body, inline css etc. IE browser compatible
            Dynamicweb.Rendering.Template htmlForPdfTemplate = new Rendering.Template("/eCom7/CartV2/Mail/pdfReceipt.cshtml");
            
            //Create an order renderer instance
            Dynamicweb.Ecommerce.Frontend.Renderer orderRenderer = new Frontend.Renderer();
            //Render all order tags
            orderRenderer.RenderOrder(sendingConfirmationMailArgs.Order, htmlForPdfTemplate);

            //Add custom tags to the template
            htmlForPdfTemplate.SetTag("RecipientEmail", sendingConfirmationMailArgs.Recipient.Address);
            htmlForPdfTemplate.SetTag("ReceiptSubject", sendingConfirmationMailArgs.MailMessage.Subject);

            //Create a pdf from the template - use another path for security reasons
            string pathToOrderConfirmation = SystemInformation.MapPath($"/Files/System/OrderExport/Order{sendingConfirmationMailArgs.Order.Id}.pdf");
            Doc theDoc = new Doc();
            theDoc.AddImageHtml(htmlForPdfTemplate.Output());
            theDoc.Save(pathToOrderConfirmation);
            theDoc.Clear();
            //Better delete the generated pdf after this...;
            //Attach the pdf to the email
            sendingConfirmationMailArgs.MailMessage.Attachments.Add(new System.Net.Mail.Attachment(pathToOrderConfirmation));
        }
      }
  }

BR Nicolai

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Be aware that this requires you to purchase a abcpdf license.

 
Aki Ruuskanen
Aki Ruuskanen
Reply

I have used this one in a couple of solutions : https://www.nuget.org/packages/Select.HtmlToPdf/

Works great.

Regards / Aki

 
Kim Søjborg Pedersen
Reply

+1 Aki Ruuskanen - Thanks, I have really been looking for at free PDF generator and there it was :)

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

Now when ABCpdf has been replaced by IronPdf is an IronPdf necessary if I want to use IronPdf in my custom code?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Yes, you need an IronPdf license to code against it.

I will never understand why the PDF is needed... All the information is already in the email - creating an attachment seems really weird - I can understand that if a remote system could only create an invoice in PDF that it had to be attached. But creating one eventhough we have the information in a more modern format, puzzles me...

PDF is old days - the glue between old envolopes and current emails...

 
Lars Larsen
Lars Larsen
Reply

Hi Nicolai

You're right. But in my scenario we send the orderconfirmation as normal in an e-mail to the customer but also to a remote system in pdf format.

 

You must be logged in to post in the forum