Hi,
How to Generate pdf After loading of page in mvc angularjs
after loading of page i need to download pdf
this what i have tried from my end
could you please check my code and help me
public ActionResult GenerateLoanPremiumPDF()
{
try
{
var preQuotaNumber = "1222";
var insuredName = "mahesh";
DateTime policyStartDate = DateTime.Now;
DateTime policyEndDate = DateTime.Now;
var planType = "aajaj";
Document pdfDoc = new Document(PageSize.LETTER, 30f, 20f, 50f, 40f);
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
iTextSharp.text.Font font1 = iTextSharp.text.FontFactory.GetFont("Segoe UI", 12, iTextSharp.text.Font.BOLD);
iTextSharp.text.Font font2 = iTextSharp.text.FontFactory.GetFont("Segoe UI", 9, iTextSharp.text.Font.BOLD);
PdfPTable table = new PdfPTable(2);
table.WidthPercentage = 100;
table.HorizontalAlignment = 0;
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
PdfPCell cell = new PdfPCell();
cell.Border = 0;
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_LEFT;
p.Add("Loan Insurance Quote No : " + preQuotaNumber + "");
cell.AddElement(p);
table.AddCell(cell);
cell = new PdfPCell();
Paragraph pg = new Paragraph();
pg.Alignment = Element.ALIGN_RIGHT;
pg.Add("Date : " + DateTime.Now + "");
cell.Border = 0;
cell.AddElement(pg);
table.AddCell(cell);
pdfDoc.Add(table);
Font fontbold = FontFactory.GetFont("Verdana", 11, Font.BOLD);
string reportHeading = "Contact ID No : ";
Chunk c = new Chunk("" + reportHeading + "", fontbold);
Paragraph pp = new Paragraph();
pp.Alignment = Element.ALIGN_CENTER;
pp.Add(c);
string To = "To,";
Paragraph p1 = new Paragraph();
Chunk c1 = new Chunk("" + To + "", fontbold);
p1.Add(c1);
pdfDoc.Add(p1);
p1 = new Paragraph();
p1.Add("\n");
pdfDoc.Add(p1);
pdfDoc.Add(new Paragraph());
string Address = insuredName;
Paragraph p2 = new Paragraph();
Chunk c2 = new Chunk("" + Address + "", fontbold);
p2.Add(c2);
pdfDoc.Add(p2);
pdfDoc.Add(new Paragraph("\n"));
string IntermediaryCode = "Intermediary Code :";
Paragraph p3 = new Paragraph();
Chunk c3 = new Chunk("" + IntermediaryCode + "", fontbold);
p3.Add(c3);
pdfDoc.Add(p3);
string IntermediaryName = "Intermediary Name : ";
Paragraph p4 = new Paragraph();
Chunk c4 = new Chunk("" + IntermediaryName + "", fontbold);
p4.Add(c4);
pdfDoc.Add(p4);
p4 = new Paragraph();
p4.Add("\n");
pdfDoc.Add(p4);
Paragraph pag = new Paragraph();
Chunk ck = new Chunk("LoanInsuranceQuote");
ck.SetUnderline(2, -3);
ck.Font.SetStyle(1);
pag.Alignment = Element.ALIGN_CENTER;
pag.Add(ck);
pdfDoc.Add(pag);
Paragraph par = new Paragraph();
par.Add("Details :");
pdfDoc.Add(par);
PdfPTable tbl = new PdfPTable(4);
tbl.WidthPercentage = 100;
tbl.HorizontalAlignment = 0;
tbl.SpacingBefore = 20f;
tbl.SpacingAfter = 30f;
tbl.AddCell("aaaaaa");
tbl.AddCell("bbbb");
tbl.AddCell("cccc");
tbl.AddCell("ddd");
pdfDoc.Add(tbl);
pdfDoc.Add(tbl);
pdfDoc.NewPage();
pdfDoc.Close();
System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename= " + preQuotaNumber + ".pdf");
System.Web.HttpContext.Current.Response.Write(pdfDoc);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
return Content("");
//return new JsonResult() { Data = new { FileName = "Report.pdf" } };
}
catch (Exception ex)
{
return null;
}
}
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', ['$scope', '$http', function ($scope, $http) {
$http({
method: 'GET',
url: '/Home/GenerateLoanPremiumPDF/'
}).success(function (data) {
$scope.items = data;
});
} ]);
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
</body>
</html>