Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Convert View into the PDF in MVC
WhatsApp
Mukesh Kumar
Oct 24
2015
3.9
k
0
0
public
ActionResult ConvertAboutPageToPdf()
{
// get the About view HTML code
string
htmlToConvert = RenderViewAsString(
"About"
,
null
);
// the base URL to resolve relative images and css
String thisPageUrl =
this
.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl = thisPageUrl.Substring(0, thisPageUrl.Length -
"Home/ConvertAboutPageToPdf"
.Length);
// instantiate the HiQPdf HTML to PDF converter
HtmlToPdf htmlToPdfConverter =
new
HtmlToPdf();
// render the HTML code as PDF in memory
byte
[] pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlToConvert, baseUrl);
// send the PDF file to browser
FileResult fileResult =
new
FileContentResult(pdfBuffer,
"application/pdf"
);
fileResult.FileDownloadName =
"AboutMvcViewToPdf.pdf"
;
return
fileResult;
}
mvc
Convert View into the PDF
C#
Up Next
Convert View into the PDF in MVC