MVC PDF Generator

kadir 60 Reputation points
2024-05-29T06:51:00.2666667+00:00

Hello everyone, friends, I will have a question for you, I am recording in mvc, when users fill the table, it assigns records to the database, there is no problem in this part, but when it comes to reporting, there is a problem, I cannot properly pdf the data entered by the user. I am sharing my codes to you in general, I would like your support in this regard.

    public ActionResult create(csbasvurular csbasvurularr)

    {

        string mesaj = "", mesajOk = "";

        if (ModelState.IsValid)

        {

                db.TBL_GenelBilgi.Add(csbasvurularr.TBL_GenelBilgi);

                csbasvurularr.TBL_GenelBilgi.isDelete = false;

                int newlyCreatedId = csbasvurularr.TBL_GenelBilgi.GenelBilgiid;

                for (int i = 0; i < csbasvurularr.TBL_YabanciDilList.Count; i++)

                {

                    var dilAdi = csbasvurularr.TBL_YabanciDilList[i].DilAdi;

                    var seviye = csbasvurularr.TBL_YabanciDilList[i].Seviye;

                    if (!string.IsNullOrEmpty(dilAdi) && !string.IsNullOrEmpty(seviye))

                    {

                        TBL_YabanciDil itemYabanciDil = new TBL_YabanciDil

                        {

                            DilAdi = dilAdi,

                            Seviye = seviye,

                            GenelBilgiid = newlyCreatedId,

                            isDelete = false

                        };

                        db.TBL_YabanciDil.Add(itemYabanciDil);

                    }

                }

                for (int i = 0; i < csbasvurularr.TBL_DigerBilgiList.Count; i++)

                {

                    TBL_DigerBilgi itemDigerBilgi = new TBL_DigerBilgi();

                    itemDigerBilgi.GenelBilgiid = newlyCreatedId;

                    itemDigerBilgi.TalepEdilenUcret = csbasvurularr.TBL_DigerBilgiList[i].TalepEdilenUcret;

                    itemDigerBilgi.BaslamaTarihi = csbasvurularr.TBL_DigerBilgiList[i].BaslamaTarihi;

                    itemDigerBilgi.İlgiAlan = csbasvurularr.TBL_DigerBilgiList[i].İlgiAlan;

                    itemDigerBilgi.UyeDernek = csbasvurularr.TBL_DigerBilgiList[i].UyeDernek;

                    itemDigerBilgi.CalismakIstedigiSehir = csbasvurularr.TBL_DigerBilgiList[i].CalismakIstedigiSehir;

                    itemDigerBilgi.SeyahatEngel = csbasvurularr.TBL_DigerBilgiList[i].SeyahatEngel;

                    itemDigerBilgi.isDelete = false;

                    db.TBL_DigerBilgi.Add(itemDigerBilgi);

                }


               

                string pdfPath = CreatePDF(csbasvurularr, newlyCreatedId);

                var pdfEntry = new TBL_Pdf

                {

                    PdfNeme = pdfPath,

                    GenelBilgiid = newlyCreatedId,

                    isDelete = false

                };

                db.TBL_Pdf.Add(pdfEntry);

                db.SaveChanges();


               

                mesajOk = "Başvurunuz alınmıştır.";

                return RedirectToAction("create", new { mesajOk });


           

        }


      

        return RedirectToAction("create", new { mesajOk });


       

    }

    public string CreatePDF(csbasvurular csbasvurularr, int? newlyCreatedId)

    {

        Document document = new Document();

        Random random = new Random();

        int randomNumber = random.Next(10000, 99999);

        string fileName = $"{csbasvurularr.TBL_GenelBilgi.Ad + "-" + csbasvurularr.TBL_GenelBilgi.Soyad}_{newlyCreatedId}_{randomNumber}.pdf";

        string path = Server.MapPath("~/pdf/" + fileName);

        PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));

        document.Open();

        // Başlık

        Font titleFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16);

        Paragraph title = new Paragraph($"{csbasvurularr.TBL_GenelBilgi.Ad} {csbasvurularr.TBL_GenelBilgi.Soyad} İş Başvurusu", titleFont);

        title.Alignment = Element.ALIGN_CENTER;

        document.Add(title);

        PdfPTable personalTable = new PdfPTable(2);

        personalTable.AddCell("Ad:");

        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Ad);

        personalTable.AddCell("Soyad:");

        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Soyad);

        personalTable.AddCell("E-Mail:");

        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Email);

        personalTable.AddCell("Telefon:");

        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Tel);

        document.Add(personalTable);


      


     

        document.Close();

        return fileName; 

    }
Developer technologies | ASP.NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2024-05-29T09:31:00.98+00:00

    Hi @kadir,

    there is a problem, I cannot properly pdf the data entered by the user.

    I briefly tested your code and it works fine. Based on your description, you should be able to successfully get the value. What does the PDF you generated look like?

    First, you can check whether the value was successfully retrieved.

    User's image

    If you cannot find the PDF file in the solution directory in VS, you will need to click "Show All Files" to display the file,then right click "Include In Project".

    User's image

    User's image

    This is the code I tested:

    public ActionResult Create()
    {
        var model = new csbasvurular();
        return View(model);
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult create(csbasvurular csbasvurularr)
    {
        string mesaj = "", mesajOk = "";
        if (ModelState.IsValid)
        {
            int newlyCreatedId = csbasvurularr.TBL_GenelBilgi.GenelBilgiid;
            string pdfPath = CreatePDF(csbasvurularr, newlyCreatedId);
            var pdfEntry = new TBL_Pdf
            {
                PdfNeme = pdfPath,
                GenelBilgiid = newlyCreatedId,
                isDelete = false
            };
            mesajOk = "Başvurunuz alınmıştır.";
            return RedirectToAction("create", new { mesajOk });
        }
        return RedirectToAction("create", new { mesajOk });
    }
    public string CreatePDF(csbasvurular csbasvurularr, int? newlyCreatedId)
    {
        Document document = new Document();
        Random random = new Random();
        int randomNumber = random.Next(10000, 99999);
        string fileName = $"{csbasvurularr.TBL_GenelBilgi.Ad + "-" + csbasvurularr.TBL_GenelBilgi.Soyad}_{newlyCreatedId}_{randomNumber}.pdf";
        string path = Server.MapPath("~/pdf/" + fileName);
        PdfWriter.GetInstance(document, new FileStream(path, FileMode.Create));
        document.Open();
        // Başlık
        Font titleFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 16);
        Paragraph title = new Paragraph($"{csbasvurularr.TBL_GenelBilgi.Ad} {csbasvurularr.TBL_GenelBilgi.Soyad} İş Başvurusu", titleFont);
        title.Alignment = Element.ALIGN_CENTER;
        document.Add(title);
        PdfPTable personalTable = new PdfPTable(2);
        personalTable.AddCell("Ad:");
        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Ad);
        personalTable.AddCell("Soyad:");
        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Soyad);
        personalTable.AddCell("E-Mail:");
        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Email);
        personalTable.AddCell("Telefon:");
        personalTable.AddCell(csbasvurularr.TBL_GenelBilgi.Tel);
        document.Add(personalTable);
        document.Close();
        return fileName;
    }
    
     public class csbasvurular
     {
         public TBL_GenelBilgi TBL_GenelBilgi {  get; set; }       
     }
     public class TBL_GenelBilgi
     {
         public int GenelBilgiid { get;  set; }
         public string Ad { get;  set; }
         public string Soyad { get;  set; }
         public string Email { get; set; }
         public string Tel { get; set; }
     }
    
    @model MVCDemo.Models.csbasvurular
    @{
        ViewBag.Title = "Create";
    }
    <h2>Create</h2>
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            <h4>TBL_GenelBilgi</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div id="education-seminar-forms">
                <div class="form-group">
                    @Html.LabelFor(model => model.TBL_GenelBilgi.GenelBilgiid, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TBL_GenelBilgi.GenelBilgiid, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.TBL_GenelBilgi.GenelBilgiid, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.TBL_GenelBilgi.Ad, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TBL_GenelBilgi.Ad, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.TBL_GenelBilgi.Ad, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.TBL_GenelBilgi.Soyad, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TBL_GenelBilgi.Soyad, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.TBL_GenelBilgi.Soyad, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.TBL_GenelBilgi.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TBL_GenelBilgi.Email, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.TBL_GenelBilgi.Email, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.TBL_GenelBilgi.Tel, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.EditorFor(model => model.TBL_GenelBilgi.Tel, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.TBL_GenelBilgi.Tel, "", new { @class = "text-danger" })
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Submit" />
                </div>
            </div>
        </div>
    }
    

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.