Hi rani,
Refer below sample code.
For display the pdf from path you need to allow access to Static Files in ASP.Net Core inside the Startup.cs class.
Refer below article for more details.
Model
public class Customer
{
public string CustomerID { get; set; }
public string ContactName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
Namespaces
using System.IO;
using System.Linq;
using iText.Html2pdf;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
Controller
public class HomeController : Controller
{
private IHostingEnvironment Environment;
private DBCtx Context { get; }
public HomeController(DBCtx _context, IHostingEnvironment _environment)
{
this.Context = _context;
Environment = _environment;
}
public IActionResult Index()
{
return View(this.Context.Customers.Take(5).ToList());
}
[HttpPost]
public IActionResult ExportPDF(string GridHtml)
{
string path = Path.Combine(this.Environment.WebRootPath, "Uploads");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
using (MemoryStream stream = new MemoryStream())
{
HtmlConverter.ConvertToPdf(GridHtml, stream);
System.IO.File.WriteAllBytes(path + "/Test.pdf", stream.ToArray());
string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"400px\" height=\"200px\">";
embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
embed += "</object>";
TempData["Embed"] = string.Format(embed, "/Uploads/Test.pdf");
return RedirectToAction("Index");
}
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@using Export_Table_PDF_Core_MVC.Models
@model IEnumerable<Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(".btnSubmit").click(function () {
$("input[name='GridHtml']").val($("#Grid").html());
});
});
</script>
</head>
<body>
<h4>Customers</h4>
<hr />
<div id="Grid">
<table cellpadding="5" cellspacing="0" style="border: 1px solid #ccc;font-size: 9pt;">
<tr>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">CustomerID</th>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">ContactName</th>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">City</th>
<th style="background-color: #B8DBFD;border: 1px solid #ccc">Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td style="width:120px;border: 1px solid #ccc">@customer.CustomerID</td>
<td style="width:120px;border: 1px solid #ccc">@customer.ContactName</td>
<td style="width:120px;border: 1px solid #ccc">@customer.City</td>
<td style="width:120px;border: 1px solid #ccc">@customer.Country</td>
</tr>
}
</table>
</div>
<br /><br />
<form asp-controller="Home" method="post">
<input type="hidden" name="GridHtml" />
<input type="submit" value="PDF" asp-action="ExportPDF" class="btnSubmit" />" />
</form>
<hr />
<div>@Html.Raw(TempData["Embed"])</div>
</body>
</html>
Screenshot