Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my code i try to download the pdf file of my view by clicking on Export button but unable to download it.
I want to download the view same as i see on my browser.
I installed iTextSharp.pdf for downloading pdf.

IMP:
1. I create a action method name GetSelectedTemplate where i get data and fetch that all data to the GetSelectedTemplate.cshtml view.
2. in that view when i click on export button i want to download the pdf (same as view -> all data and design (like a scrrenshot)) of view.
I write Pdf downloading code in another action method "Export".

What I have tried:

ASP.NET
Controller: <pre>[HttpGet]
		public async Task<ActionResult> GetSelectedTemplate(int TempId)
		{
			JobSeeker objJ = new JobSeeker();
			BALJobSeeker objBAL = new BALJobSeeker();
			List<JobSeeker> jobSeekers = new List<JobSeeker>();
			objJ.TemplateId = TempId;
			SqlDataReader dr = await objBAL.TemplateNameDetails(objJ);
			// Retrieve lists from session
			ViewBag.BasicList = HttpContext.Session["BasicList"];
			ViewBag.QualificationList = HttpContext.Session["QualificationList"];
			ViewBag.EmployementList = HttpContext.Session["EmployementList"];
			ViewBag.ProjectList = HttpContext.Session["ProjectList"];
			ViewBag.CertificationList = HttpContext.Session["CertificationList"];
			ViewBag.SkillsExperienceList = HttpContext.Session["SkillsExperienceList"];
			// Retrieve the objective value from the query string
			string objective = Request.QueryString["ProfileSummary"];
			// Pass ObjectiveBag data to another controller through the TempData.
			//TempData["ObjectiveBag"] = objective;
			ViewBag.ObjectiveBag = TempData["ObjectiveBag"];
			if (await dr.ReadAsync())
			{
				string templateName = dr["TemplateName"].ToString();
				if (templateName == "Resume_Template_1")
				{
					return View("GetSelectedTemplate");
				}
				else if (templateName == "Resume_Template_3")
				{
					return View("GetSelectedTemplate2");
				}
				else if(templateName == "Resume_Template_2")
				{
					return View("GetSelectedTemplate3");
				}
				else
				{
					return HttpNotFound();
				}
			}
			return View();
		}
		[HttpPost]
		[ValidateInput(false)]
		public FileResult Export(string PdfHtml)
		{
			using (MemoryStream stream = new MemoryStream())
			{
				using (StringReader sr = new StringReader(PdfHtml))
				{
					Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
					PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
					pdfDoc.Open();
					XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
					pdfDoc.Close();
				}
				return File(stream.ToArray(), "application/pdf", "Resume.pdf");
			}
		}

View Code:
Razor
<pre>@{
    Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Gaya Templates</title>
    <!-- Meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Responsive Resume Template">
    <meta name="author" content="Xiaoying Riley at 3rd Wave Media">
    <link rel="shortcut icon" href="favicon.ico">
    <!-- Google Font -->
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,900" rel="stylesheet">
    <!-- FontAwesome JS-->
    <script defer src="~/Content/JobSeeker/CSS/fontawesome/js/all.min.js"></script>
    <!-- Theme CSS -->
    <link id="theme-style" rel="stylesheet" href="~/Content/JobSeeker/CSS/pillar-1.css">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnSubmit").click(function () {
                $("#PdfHtml").val($("#resumeContent").html());
            });
        });
    </script>
</head>
<body>
    @using (Html.BeginForm("Export", "JobSeeker", FormMethod.Post))
    {
        <input type="hidden" name="PdfHtml" id="PdfHtml" />
        <input type="submit" id="btnSubmit" value="Export" />
        <article id="resumeContent" class="resume-wrapper text-center position-relative">
            <div class="resume-wrapper-inner mx-auto text-start bg-white shadow-lg">
                <header class="resume-header pt-4 pt-md-0">
                    <div class="row">
                        @foreach (var jobSeeker in ViewBag.BasicList)
                        {
                            <div class="col-block col-md-auto resume-picture-holder text-center text-md-start">
                                @{

                                    var photopath = "../Content/JobSeeker/Image/";
                                    var imagePath = string.Format("{0}{1}", photopath, jobSeeker.Photo);
                                }
                                <img class="picture" src="@imagePath" alt="" style="height:220px;" />
                            </div>
                            <div class="col">
                                <div class="row p-4 justify-content-center justify-content-md-between">
                                    <div class="primary-info col-auto">
                                        <h1 class="name mt-0 mb-1 text-white text-uppercase text-uppercase">@jobSeeker.FullName</h1>
                                        @*<div class="title mb-3">Full Stack Developer</div>*@
                                        <ul class="list-unstyled">
                                            <li><a class="text-link" href="#">@jobSeeker.MobileNo</a></li>
                                        </ul>
                                    </div><!--//primary-info-->
                                    <div class="secondary-info col-auto mt-2">
                                        <ul class="resume-social list-unstyled">
                                            <li class="mb-3"><a class="text-link" href="#">^__i class="fab fa-linkedin-in fa-fw">@jobSeeker.ProfileUrl</a></li>
                                            <li class="mb-3"><a class="text-link" href="#">^__i class="fas fa-user fa-fw">@jobSeeker.Gender</a></li>
                                            <li><a class="text-link" href="#">^__i class="fas fa-globe">yourwebsite.com</a></li>
                                        </ul>
                                    </div><!--//secondary-info-->
                                </div><!--//row-->

                            </div><!--//col-->
                        }
                    </div><!--//row-->

                </header>
                <div class="resume-body p-5">
                    <section class="resume-section summary-section mb-5">
                        <h2 class="resume-section-title text-uppercase font-weight-bold pb-3 mb-3">Career Summary</h2>
                        <div class="resume-section-content">
                            <p class="mb-0"> @ViewBag.ObjectiveBag<a class="text-reset text-link" href="https://themes.3rdwavemedia.com/resources/sketch-template/pillar-sketch-sketch-resume-template-for-developers/" target="_blank"></a></p>
                        </div>
                    </section><!--//summary-section-->
                    
                </div><!--//resume-body-->
            </div>
        </article>
    }

</body>
</html>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900