PDF from MVC Razor - with heading
Today I was tasked with creating a PDF from a report that had two queries - a header section and detail section.
After looking at all the viable options I was tempted by iTextSharp but was faced with the challenge of implementing it in razor. I then came across this nifty dll written specifically for that purpose and it utilizes iTextSharp to do it.
RazorPDF was the answer. Al Nyveldt did an excellent job with it and includes a github download with great samples. The one challenge he didn't cover was my example of two queries to make a more comprehensive report. As shown in the example (screenshot of my pdf generated using this tool) I have header information as well as additional data laid out in a table.

To accomplish this I created a ViewModel in my Data project with a simple class that incorporated two references. One to the header data and one to the Zipcode detail.
public class Fran_Zip_ViewModel
{
public IEnumerable FranZip { get; set; }
public IEnumerable FranFran { get; set; }
}
Then in my page I based my model on this class
@model MyProject.Data.DTO.Fran_Zip_ViewModel
In the controller I created an object for each grouping of data:
var list = new List();
list = bl.GetFranchiseeHeader(actcode).ToList();
var retdata = bl.GetFranchiseeTerritories(actcode, "1", "0");
var itemsRtoreturn = retdata.ToList();
//Here is where I put them together
var allitems = new MyProject.Data.DTO.Fran_Zip_ViewModel { FranFran = list, FranZip = itemsRtoreturn };
And of course the ability to output as PDF (for details see the link to RazorPDF)
// Output to Pdf
if (Request.QueryString["format"] == "pdf")
return new PdfResult(allitems, "TerritoryPDF");
To use them in my view I looped over each section with a foreach and a reference on the model to the specific dataset
foreach (var item in Model.FranFran){@Html.DisplayFor(model => item.Country)... etc}
@foreach (var item in Model.FranZip) { and same thing here}
MVC always seems to have elegant solutions - just sometimes challenging when first starting to figure out the most efficient way to do things that seemed easier before MVC.
By the way I covered this topic a couple of months ago in this post and this stuff is starting to come more naturally.
After looking at all the viable options I was tempted by iTextSharp but was faced with the challenge of implementing it in razor. I then came across this nifty dll written specifically for that purpose and it utilizes iTextSharp to do it.
RazorPDF was the answer. Al Nyveldt did an excellent job with it and includes a github download with great samples. The one challenge he didn't cover was my example of two queries to make a more comprehensive report. As shown in the example (screenshot of my pdf generated using this tool) I have header information as well as additional data laid out in a table.
To accomplish this I created a ViewModel in my Data project with a simple class that incorporated two references. One to the header data and one to the Zipcode detail.
public class Fran_Zip_ViewModel
{
public IEnumerable
public IEnumerable
}
Then in my page I based my model on this class
@model MyProject.Data.DTO.Fran_Zip_ViewModel
In the controller I created an object for each grouping of data:
var list = new List
list = bl.GetFranchiseeHeader(actcode).ToList();
var retdata = bl.GetFranchiseeTerritories(actcode, "1", "0");
var itemsRtoreturn = retdata.ToList();
//Here is where I put them together
var allitems = new MyProject.Data.DTO.Fran_Zip_ViewModel { FranFran = list, FranZip = itemsRtoreturn };
And of course the ability to output as PDF (for details see the link to RazorPDF)
// Output to Pdf
if (Request.QueryString["format"] == "pdf")
return new PdfResult(allitems, "TerritoryPDF");
To use them in my view I looped over each section with a foreach and a reference on the model to the specific dataset
foreach (var item in Model.FranFran){@Html.DisplayFor(model => item.Country)... etc}
@foreach (var item in Model.FranZip) { and same thing here}
MVC always seems to have elegant solutions - just sometimes challenging when first starting to figure out the most efficient way to do things that seemed easier before MVC.
By the way I covered this topic a couple of months ago in this post and this stuff is starting to come more naturally.
Comments