Reputation: 1074
I am a beginner in MVC and have a requirement to generate PDF of invoices on provided template. After doing a bit of googling, now I am able to generate a pdf but not in Template. Can any body help me on this. I am writing my code here below:
public ActionResult pdfStatement(string InvoiceNumber)
{
InvoiceNumber = InvoiceNumber.Trim();
ObjectParameter[] parameters = new ObjectParameter[1];
parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);
var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
Models.Statement statement = statementResult.SingleOrDefault();
return ViewPdf("Invoice", "pdfStatement", statement);
}
public class PdfViewController : Controller
{
private readonly HtmlViewRenderer htmlViewRenderer;
private readonly StandardPdfRenderer standardPdfRenderer;
public PdfViewController()
{
this.htmlViewRenderer = new HtmlViewRenderer();
this.standardPdfRenderer = new StandardPdfRenderer();
}
protected ActionResult ViewPdf(string pageTitle, string viewName, object model)
{
// Render the view html to a string.
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
// Let the html be rendered into a PDF document through iTextSharp.
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
// Return the PDF as a binary stream to the client.
return new BinaryContentResult(buffer, "application/pdf");
}
}
public class BinaryContentResult : ActionResult
{
private readonly string contentType;
private readonly byte[] contentBytes;
public BinaryContentResult(byte[] contentBytes, string contentType)
{
this.contentBytes = contentBytes;
this.contentType = contentType;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.Clear();
response.Cache.SetCacheability(HttpCacheability.Public);
response.ContentType = this.contentType;
using (var stream = new MemoryStream(this.contentBytes))
{
stream.WriteTo(response.OutputStream);
stream.Flush();
}
}
}
Upvotes: 4
Views: 14196
Reputation: 485
Try this ejsreport
It uses JSON and Javascript but gets the job done.
I have been trying to look for a simpler reporting tool in MVC and that's the best I found.
Upvotes: 0
Reputation: 584
it's not an answer for question, but my experience about pdf-generating in asp.net. Maybe it will save your time. Unfortunately i did not found free good enough tools to generate pdf files.
I tried to use HtmlViewRenderer, but it does not works for me for with complex css. Than i found pdfsharp. There are a lot o good articles of its using of StackOverFlow.
This helps me to create invoices - simple table, but i want to warn, you would add rows by your hands. And that code does not looks very good. Also it does not working for complex css.
For big colorful reports now we are using PdfCrowd. It's a service that renders your html as pdf. It works perfect, but not free, the cheapest plan costs 10$ per year.
On official site you can find .net library and examples for ASP.NET.
Upvotes: 4
Reputation: 3812
I would recommend the iTextSharp library.
Check out this tutorial on how to populate a pdf from c# using the library. Create a pdf with fields in Adobe Acrobat, and then populate the fields from code.
// Open the template pdf
PdfReader pdfReader = new PdfReader(Request.MapPath("~/assets/form.pdf"));
PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
pdfStamper.FormFlattening = true; // generate a flat PDF
// Populate the fields
AcroFields pdfForm = pdfStamper.AcroFields;
pdfForm.SetField("InvoiceRef", "00000");
pdfForm.SetField("DeliveryAddress", "Oxford Street, London");
pdfForm.SetField("Email", "[email protected]");
pdfStamper.Close();
Upvotes: 6