Technoligent professionals have shared many tutorials to make the job of asp.net developers easy. In this article, they will explain how to generate PDF file from MVC page and send email using generated PDF as attachment. Read and learn step-by-step in this tutorial and make best practices for asp.net development. They have also shared the things you may need before commencing the project.
For this task I will create one partial view in MVC 4 project that contains one text box and some questions with two options. Also, I am using Rotativa library to convert .cshtml page to PDF, you can get this library from NuGet package.
Add following code to GeneratePDFModel class as below and inherit PartialViewAsPdf class from Rotativa library. It is a main class which generate PDF file from .cshtml page. Main purpose of this code is the PartialViewAsPdf will return a PDF file, but we want a byte array to create attachment file for email.
To include Rotativa library in your project execute following command to Package Manager Console.
Next step is to add following key values to
Add Index view as below and update design as per requirement.
Then add Partial view to Home folder and design as below.
Now, create ActionResult which execute HttpPost method for Index page in Home Controller with model as input parameter.
Above code will get content from Partial view and generate PDF file using ViewAsPdf2 method then send email to specified email address with newly generated PDF file as attachment.
And following is the pdf file screenshot sent as attachment with email
You can update partial view design as per your requirement which effects content of pdf file. All the steps shared by Technoligent experts are essential to create PDF file from MVC page. You also need the things required for preparing PDFs (that is also mentioned in the post). You may share your review for this tutorial or ask query related to the subject from ASP.Net development services. Don’t forget to comment in the below section.
Technoligent is prominent name in IT industry having stable team of resources working on various technologies from decades. Our full fledged team of resources assures experts solutions for top IT industries in India and USA. Today, we are giving quality IT outsourcing services to our clients with wonderful exposure to different IT domains.
We work together with Technoligent in a mobile Project. It was not easy to develop, but the Team of Technoligent was very professional and we did a perfect solution for the european Sailors. Now we look forward to realise the next Projects together... Moreby Jorg B (Austria)
We found a reliable and efficient partner in Technoligent, ready to support our strategy of offshore software development. Technoligent has particularly impressed by the speed of response to any request for the development of software applications and for their maintenance... Moreby Fabio Durso