Files
.vs
Fab2ApprovalSystem
Fab2ApprovalSystem-Copy
App_Start
Content
Controllers
DMO
EmailTemplates
FTPBatch
Lib
Misc
Models
PdfGenerator
BinaryContentResult.cs
FakeView.cs
HtmlViewRenderer.cs
PdfViewController.cs
PrintHeaderFooter.cs
StandardPdfRenderer.cs
Properties
Scripts
Utilities
ViewModels
Views
fonts
Fab2ApprovalSystem.csproj
Fab2ApprovalSystem.csproj.user
Fab2ApprovalSystem.csproj.vspscc
Global.asax
Global.asax.cs
Project_Readme.html
Startup.cs
Test.html
Web.Debug.config
Web.Release.config
Web.config
favicon.ico
packages.config
Fab2ApprovalSystem-TF
Kendo
SQL
packages
references
.editorconfig
Fab2ApprovalSystem.sln
README.md
mesa-fab-approval/Fab2ApprovalSystem-Copy/PdfGenerator/PdfViewController.cs
Jonathan Ouellette 580e90f6a2 initial add
2022-09-27 14:10:30 -07:00

69 lines
2.5 KiB
C#

// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PdfViewController.cs" company="SemanticArchitecture">
// http://www.SemanticArchitecture.net pkalkie@gmail.com
// </copyright>
// <summary>
// Extends the controller with functionality for rendering PDF views
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Fab2ApprovalSystem.PdfGenerator
{
using System.Web.Mvc;
using System.IO;
/// <summary>
/// Extends the controller with functionality for rendering PDF views
/// </summary>
public class PdfViewController : Controller
{
private readonly HtmlViewRenderer htmlViewRenderer;
private readonly StandardPdfRenderer standardPdfRenderer;
public PdfViewController()
{
this.htmlViewRenderer = new HtmlViewRenderer();
this.standardPdfRenderer = new StandardPdfRenderer();
}
/// <summary>
///
/// </summary>
/// <param name="pageTitle"></param>
/// <param name="viewName"></param>
/// <param name="model"></param>
/// <returns></returns>
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");
}
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="viewName"></param>
/// <param name="model"></param>
protected void SavePdf(string fileName, 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, "");
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(buffer, 0, buffer.Length);
}
}
}
}