DEV Community

Supraja Tangella
Supraja Tangella

Posted on

🚀 𝗛𝗼𝘄 𝘁𝗼 𝗔𝘁𝘁𝗮𝗰𝗵 𝗮𝗻𝗱 𝗦𝗲𝗻𝗱 𝗮𝗻 𝗘𝘅𝗽𝗲𝗻𝘀𝗲 𝗥𝗲𝗽𝗼𝗿𝘁 𝗣𝗗𝗙 𝘃𝗶𝗮 𝗘𝗺𝗮𝗶𝗹 𝗶𝗻 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲?

How can we generate an 𝗘𝘅𝗽𝗲𝗻𝘀𝗲 𝗥𝗲𝗽𝗼𝗿t 𝗣𝗗𝗙 and send it as an 𝗲𝗺𝗮𝗶𝗹 𝗮𝘁𝘁𝗮𝗰𝗵𝗺𝗲𝗻𝘁 using 𝗚𝗺𝗮𝗶𝗹 (𝗙𝗿𝗲𝗲 𝗣𝗹𝗮𝗻) in an 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲 𝗪𝗲𝗯 𝗔𝗽𝗽?

1️⃣ 𝗜𝗻𝘀𝘁𝗮𝗹𝗹 𝗥𝗲𝗾𝘂𝗶𝗿𝗲𝗱 𝗣𝗮𝗰𝗸𝗮𝗴𝗲𝘀

Install-Package iTextSharp.LGPLv2.Core # PDF Generation

Install-Package MailKit # Email Sending

2️⃣ 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗲 𝘁𝗵𝗲 𝗣𝗗𝗙

public byte[] GenerateExpensePdf(List expenses)
{
using var ms = new MemoryStream();
var doc = new Document();
PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.Add(new Paragraph("Expense Report - " + DateTime.Now.ToString("MMMM yyyy")));
var table = new PdfPTable(3) { WidthPercentage = 100 };
table.AddCell("Date"); table.AddCell("Category"); table.AddCell("Amount ($)");
expenses.ForEach(e => { table.AddCell(e.Date.ToString("dd-MM-yyyy")); table.AddCell(e.Category); table.AddCell(e.Amount.ToString("F2")); });
doc.Add(table); doc.Close();
return ms.ToArray();
}

3️⃣ 𝗦𝗲𝗻𝗱 𝗘𝗺𝗮𝗶𝗹 𝘄𝗶𝘁𝗵 𝗣𝗗𝗙 𝗔𝘁𝘁𝗮𝗰𝗵𝗺𝗲𝗻𝘁

public async Task SendExpenseReportByEmail(string recipient, byte[] pdfBytes)
{
var message = new MimeMessage { Subject = "Your Expense Report" };
message.From.Add(new MailboxAddress("Expense Manager", "your-email@gmail.com"));
message.To.Add(new MailboxAddress("", recipient));
var body = new TextPart("plain") { Text = "Please find your expense report attached." };
var attachment = new MimePart("application", "pdf") { Content = new MimeContent(new MemoryStream(pdfBytes)), FileName = "ExpenseReport.pdf" };
message.Body = new Multipart("mixed") { body, attachment };

using var client = new SmtpClient();
await client.ConnectAsync("smtp.gmail.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
await client.AuthenticateAsync("your-email@gmail.com", "your-app-password");
await client.SendAsync(message); await client.DisconnectAsync(true);
Enter fullscreen mode Exit fullscreen mode

}

✅ 𝗚𝗲𝗻𝗲𝗿𝗮𝘁𝗲𝘀 𝗣𝗗𝗙 📄

✅ 𝗔𝘁𝘁𝗮𝗰𝗵𝗲𝘀 & 𝗦𝗲𝗻𝗱𝘀 𝘃𝗶𝗮 𝗚𝗺𝗮𝗶𝗹 ✉️

✅ 𝗪𝗼𝗿𝗸𝘀 𝗶𝗻 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲 𝗪𝗲𝗯 𝗔𝗽𝗽

𝗪𝗼𝘂𝗹𝗱 𝘆𝗼𝘂 𝗶𝗺𝗽𝗹𝗲𝗺𝗲𝗻𝘁 𝘁𝗵𝗶𝘀 𝗶𝗻 𝘆𝗼𝘂𝗿 𝗽𝗿𝗼𝗷𝗲𝗰𝘁? 𝗟𝗲𝘁 𝗺𝗲 𝗸𝗻𝗼𝘄! 👇😊

𝗔𝗦𝗣𝗡𝗘𝗧𝗖𝗼𝗿𝗲 #𝗘𝗺𝗮𝗶𝗹𝗔𝘁𝘁𝗮𝗰𝗵𝗺𝗲𝗻𝘁 #𝗚𝗺𝗮𝗶𝗹𝗦𝗠𝗧𝗣 #𝗘𝘅𝗽𝗲𝗻𝘀𝗲𝗠𝗮𝗻𝗮𝗴𝗲𝗺𝗲𝗻𝘁 #𝗖𝗦𝗵𝗮𝗿𝗽

Quadratic AI

Quadratic AI – The Spreadsheet with AI, Code, and Connections

  • AI-Powered Insights: Ask questions in plain English and get instant visualizations
  • Multi-Language Support: Seamlessly switch between Python, SQL, and JavaScript in one workspace
  • Zero Setup Required: Connect to databases or drag-and-drop files straight from your browser
  • Live Collaboration: Work together in real-time, no matter where your team is located
  • Beyond Formulas: Tackle complex analysis that traditional spreadsheets can't handle

Get started for free.

Watch The Demo 📊✨

Top comments (0)

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay