generate pdf in template using ASP.net, C#, MVC

13,173

Solution 1

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(); 

Solution 2

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.

Share:
13,173
14578446
Author by

14578446

Updated on June 04, 2022

Comments

  • 14578446
    14578446 about 2 years

    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();
            }
        }
    }