DEV Community

Supraja Tangella
Supraja Tangella

Posted on

๐—š๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ฎ๐˜๐—ถ๐—ป๐—ด ๐—ฎ ๐— ๐—ผ๐—ป๐˜๐—ต๐—น๐˜† ๐—˜๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ ๐—ฅ๐—ฒ๐—ฝ๐—ผ๐—ฟ๐˜ ๐—ณ๐—ผ๐—ฟ ๐—ฎ ๐—จ๐˜€๐—ฒ๐—ฟ ๐—ถ๐—ป ๐—–# ๐—จ๐˜€๐—ถ๐—ป๐—ด ๐—ถ๐—ง๐—ฒ๐˜…๐˜๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฝ ๐Ÿ“„

In my ๐—ฝ๐—ฒ๐—ฟ๐˜€๐—ผ๐—ป๐—ฎ๐—น ๐—ฝ๐—ฟ๐—ผ๐—ท๐—ฒ๐—ฐ๐˜ โ€“ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ ๐— ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ฆ๐˜†๐˜€๐˜๐—ฒ๐—บ, I wanted to generate an ๐—ฒ๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ ๐—ฟ๐—ฒ๐—ฝ๐—ผ๐—ฟ๐˜ for a ๐˜€๐—ฝ๐—ฒ๐—ฐ๐—ถ๐—ณ๐—ถ๐—ฐ ๐˜‚๐˜€๐—ฒ๐—ฟ for the ๐—ฐ๐˜‚๐—ฟ๐—ฟ๐—ฒ๐—ป๐˜ ๐—บ๐—ผ๐—ป๐˜๐—ต. I explored ๐—ถ๐—ง๐—ฒ๐˜…๐˜๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฝ, a free library for generating PDFs in C#. Hereโ€™s how I implemented it! ๐Ÿš€

๐Ÿ”น ๐—ฆ๐˜๐—ฒ๐—ฝ๐˜€ ๐˜๐—ผ ๐—š๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ฎ๐˜๐—ฒ ๐˜๐—ต๐—ฒ ๐—ฃ๐——๐—™ ๐—ฅ๐—ฒ๐—ฝ๐—ผ๐—ฟ๐˜

1๏ธโƒฃ ๐—™๐—ถ๐—น๐˜๐—ฒ๐—ฟ ๐—˜๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐˜๐—ต๐—ฒ ๐—–๐˜‚๐—ฟ๐—ฟ๐—ฒ๐—ป๐˜ ๐— ๐—ผ๐—ป๐˜๐—ต

First, we fetch the ๐—น๐—ผ๐—ด๐—ด๐—ฒ๐—ฑ-๐—ถ๐—ป ๐˜‚๐˜€๐—ฒ๐—ฟโ€™๐˜€ ๐—ฒ๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ๐˜€ from the database for the current month.

var userId = "User123"; // Replace with the actual logged-in user ID
var currentMonth = DateTime.Now.Month;
var currentYear = DateTime.Now.Year;

var expenses = _context.Expenses
.Where(e => e.UserId == userId && e.Date.Month == currentMonth && e.Date.Year == currentYear)
.ToList();

2๏ธโƒฃ ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐—ฎ ๐—ฃ๐——๐—™ ๐——๐—ผ๐—ฐ๐˜‚๐—บ๐—ฒ๐—ป๐˜ ๐˜„๐—ถ๐˜๐—ต ๐—ถ๐—ง๐—ฒ๐˜…๐˜๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฝ

Now, we create a ๐—ฃ๐——๐—™ ๐—ณ๐—ถ๐—น๐—ฒ and add expense details.

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;

public IActionResult GenerateExpenseReport()
{
using (MemoryStream ms = new MemoryStream())
{
Document document = new Document();
PdfWriter.GetInstance(document, ms);
document.Open();

    // Add title
    document.Add(new Paragraph("Expense Report - " + DateTime.Now.ToString("MMMM yyyy")));
    document.Add(new Paragraph("User ID: " + userId));
    document.Add(new Paragraph("\n"));

    // Add expenses in a table format
    PdfPTable table = new PdfPTable(3);
    table.AddCell("Date");
    table.AddCell("Description");
    table.AddCell("Amount");

    foreach (var expense in expenses)
    {
        table.AddCell(expense.Date.ToShortDateString());
        table.AddCell(expense.Description);
        table.AddCell(expense.Amount.ToString("C"));
    }

    document.Add(table);
    document.Close();

    return File(ms.ToArray(), "application/pdf", "ExpenseReport.pdf");
}
Enter fullscreen mode Exit fullscreen mode

}

3๏ธโƒฃ ๐——๐—ผ๐˜„๐—ป๐—น๐—ผ๐—ฎ๐—ฑ ๐˜๐—ต๐—ฒ ๐—ฃ๐——๐—™

The ๐—ด๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ฎ๐˜๐—ฒ๐—ฑ ๐—ฃ๐——๐—™ will contain the ๐˜‚๐˜€๐—ฒ๐—ฟโ€™๐˜€ ๐—ฒ๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ๐˜€ ๐—ณ๐—ผ๐—ฟ ๐˜๐—ต๐—ฒ ๐—ฐ๐˜‚๐—ฟ๐—ฟ๐—ฒ๐—ป๐˜ ๐—บ๐—ผ๐—ป๐˜๐—ต and will be available for download.

๐Ÿ”น ๐—ช๐—ต๐˜† ๐—ถ๐—ง๐—ฒ๐˜…๐˜๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฝ?

โœ… ๐—Ÿ๐—ถ๐—ด๐—ต๐˜๐˜„๐—ฒ๐—ถ๐—ด๐—ต๐˜ & ๐—ณ๐—ฟ๐—ฒ๐—ฒ (great for small projects).

โœ… ๐—ฆ๐—ถ๐—บ๐—ฝ๐—น๐—ฒ ๐˜๐—ผ ๐˜‚๐˜€๐—ฒ โ€“ generate PDFs with just a few lines of code.

โœ… ๐—–๐˜‚๐˜€๐˜๐—ผ๐—บ๐—ถ๐˜‡๐—ฎ๐—ฏ๐—น๐—ฒ โ€“ you can format the report as needed.

๐—ง๐—ต๐—ถ๐˜€ ๐—ฎ๐—ฝ๐—ฝ๐—ฟ๐—ผ๐—ฎ๐—ฐ๐—ต ๐—ต๐—ฒ๐—น๐—ฝ๐—ฒ๐—ฑ ๐—บ๐—ฒ ๐—ด๐—ฒ๐—ป๐—ฒ๐—ฟ๐—ฎ๐˜๐—ฒ ๐—บ๐—ผ๐—ป๐˜๐—ต๐—น๐˜† ๐—ฟ๐—ฒ๐—ฝ๐—ผ๐—ฟ๐˜๐˜€ ๐—ถ๐—ป ๐—บ๐˜† ๐—˜๐˜…๐—ฝ๐—ฒ๐—ป๐˜€๐—ฒ ๐— ๐—ฎ๐—ป๐—ฎ๐—ด๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ฆ๐˜†๐˜€๐˜๐—ฒ๐—บ. ๐—ช๐—ต๐—ฎ๐˜ ๐—ฑ๐—ผ ๐˜†๐—ผ๐˜‚ ๐˜๐—ต๐—ถ๐—ป๐—ธ? ๐—›๐—ฎ๐˜ƒ๐—ฒ ๐˜†๐—ผ๐˜‚ ๐˜‚๐˜€๐—ฒ๐—ฑ ๐—ถ๐—ง๐—ฒ๐˜…๐˜๐—ฆ๐—ต๐—ฎ๐—ฟ๐—ฝ ๐—ฏ๐—ฒ๐—ณ๐—ผ๐—ฟ๐—ฒ? ๐—Ÿ๐—ฒ๐˜โ€™๐˜€ ๐—ฑ๐—ถ๐˜€๐—ฐ๐˜‚๐˜€๐˜€ ๐—ถ๐—ป ๐˜๐—ต๐—ฒ ๐—ฐ๐—ผ๐—บ๐—บ๐—ฒ๐—ป๐˜๐˜€! ๐Ÿ’ฌ๐Ÿ‘‡

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong ยท web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video ๐ŸŒถ๏ธ๐Ÿ”ฅ

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someoneโ€™s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay