Tasks 184281, 184799, 184800, 184801 and 184802
Align .editorconfig files Move Controller logic to DMO classes GlobalVars.AppSettings = Models.AppSettings.GetFromConfigurationManager(); Question EditorConfig Project level editorconfig Format White Spaces AppSetting when EnvironmentVariable not set Corrective Actions Tests Schedule Actions Tests DMO Tests Controller Tests Get ready to use VSCode IDE
This commit is contained in:
@ -1,4 +1,6 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
#if !NET8
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="BinaryContentResult.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
// </copyright>
|
||||
@ -7,8 +9,7 @@
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator
|
||||
{
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
@ -16,29 +17,27 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
/// <summary>
|
||||
/// An ActionResult used to send binary data to the browser.
|
||||
/// </summary>
|
||||
public class BinaryContentResult : ActionResult
|
||||
{
|
||||
public class BinaryContentResult : ActionResult {
|
||||
private readonly string contentType;
|
||||
private readonly byte[] contentBytes;
|
||||
|
||||
public BinaryContentResult(byte[] contentBytes, string contentType)
|
||||
{
|
||||
public BinaryContentResult(byte[] contentBytes, string contentType) {
|
||||
this.contentBytes = contentBytes;
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
public override void ExecuteResult(ControllerContext context)
|
||||
{
|
||||
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))
|
||||
{
|
||||
using (var stream = new MemoryStream(this.contentBytes)) {
|
||||
stream.WriteTo(response.OutputStream);
|
||||
stream.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,3 +1,5 @@
|
||||
#if !NET8
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="FakeView.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
@ -7,21 +9,20 @@
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator
|
||||
{
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Web.Mvc;
|
||||
|
||||
public class FakeView : IView
|
||||
{
|
||||
public class FakeView : IView {
|
||||
#region IView Members
|
||||
|
||||
public void Render(ViewContext viewContext, TextWriter writer)
|
||||
{
|
||||
public void Render(ViewContext viewContext, TextWriter writer) {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,3 +1,5 @@
|
||||
#if !NET8
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="HtmlViewRenderer.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
@ -7,8 +9,7 @@
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator
|
||||
{
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
@ -18,13 +19,10 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
/// <summary>
|
||||
/// This class is responsible for rendering a HTML view into a string.
|
||||
/// </summary>
|
||||
public class HtmlViewRenderer
|
||||
{
|
||||
public string RenderViewToString(Controller controller, string viewName, object viewData)
|
||||
{
|
||||
public class HtmlViewRenderer {
|
||||
public string RenderViewToString(Controller controller, string viewName, object viewData) {
|
||||
var renderedView = new StringBuilder();
|
||||
using (var responseWriter = new StringWriter(renderedView))
|
||||
{
|
||||
using (var responseWriter = new StringWriter(renderedView)) {
|
||||
var fakeResponse = new HttpResponse(responseWriter);
|
||||
var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse);
|
||||
var fakeControllerContext = new ControllerContext(new HttpContextWrapper(fakeContext), controller.ControllerContext.RouteData, controller.ControllerContext.Controller);
|
||||
@ -32,8 +30,7 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
var oldContext = HttpContext.Current;
|
||||
HttpContext.Current = fakeContext;
|
||||
|
||||
using (var viewPage = new ViewPage())
|
||||
{
|
||||
using (var viewPage = new ViewPage()) {
|
||||
var html = new HtmlHelper(CreateViewContext(responseWriter, fakeControllerContext), viewPage);
|
||||
html.RenderPartial(viewName, viewData);
|
||||
HttpContext.Current = oldContext;
|
||||
@ -43,9 +40,10 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
return renderedView.ToString();
|
||||
}
|
||||
|
||||
private static ViewContext CreateViewContext(TextWriter responseWriter, ControllerContext fakeControllerContext)
|
||||
{
|
||||
private static ViewContext CreateViewContext(TextWriter responseWriter, ControllerContext fakeControllerContext) {
|
||||
return new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), responseWriter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,4 +1,6 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
#if !NET8
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// <copyright file="PdfViewController.cs" company="SemanticArchitecture">
|
||||
// http://www.SemanticArchitecture.net pkalkie@gmail.com
|
||||
// </copyright>
|
||||
@ -7,21 +9,18 @@
|
||||
// </summary>
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator
|
||||
{
|
||||
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
|
||||
{
|
||||
public class PdfViewController : Controller {
|
||||
private readonly HtmlViewRenderer htmlViewRenderer;
|
||||
private readonly StandardPdfRenderer standardPdfRenderer;
|
||||
|
||||
public PdfViewController()
|
||||
{
|
||||
public PdfViewController() {
|
||||
this.htmlViewRenderer = new HtmlViewRenderer();
|
||||
this.standardPdfRenderer = new StandardPdfRenderer();
|
||||
}
|
||||
@ -29,12 +28,11 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
/// <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)
|
||||
{
|
||||
protected ActionResult ViewPdf(string pageTitle, string viewName, object model) {
|
||||
// Render the view html to a string.
|
||||
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
|
||||
|
||||
@ -48,22 +46,22 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="viewName"></param>
|
||||
/// <param name="model"></param>
|
||||
protected void SavePdf(string fileName, string viewName, object model)
|
||||
{
|
||||
|
||||
|
||||
|
||||
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))
|
||||
{
|
||||
using (FileStream fs = new FileStream(fileName, FileMode.Create)) {
|
||||
fs.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
@ -1,17 +1,16 @@
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator
|
||||
{
|
||||
using System;
|
||||
|
||||
#if !NET8
|
||||
namespace Fab2ApprovalSystem.PdfGenerator {
|
||||
using iTextSharp.text;
|
||||
using iTextSharp.text.pdf;
|
||||
|
||||
using System;
|
||||
|
||||
/// <summary>
|
||||
/// This class represents the standard header and footer for a PDF print.
|
||||
/// application.
|
||||
/// </summary>
|
||||
public class PrintHeaderFooter : PdfPageEventHelper
|
||||
{
|
||||
public class PrintHeaderFooter : PdfPageEventHelper {
|
||||
private PdfContentByte pdfContent;
|
||||
private PdfTemplate pageNumberTemplate;
|
||||
private BaseFont baseFont;
|
||||
@ -19,22 +18,19 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
|
||||
public string Title { get; set; }
|
||||
|
||||
public override void OnOpenDocument(PdfWriter writer, Document document)
|
||||
{
|
||||
public override void OnOpenDocument(PdfWriter writer, Document document) {
|
||||
printTime = DateTime.Now;
|
||||
baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
|
||||
pdfContent = writer.DirectContent;
|
||||
pageNumberTemplate = pdfContent.CreateTemplate(50, 50);
|
||||
}
|
||||
|
||||
public override void OnStartPage(PdfWriter writer, Document document)
|
||||
{
|
||||
public override void OnStartPage(PdfWriter writer, Document document) {
|
||||
base.OnStartPage(writer, document);
|
||||
|
||||
Rectangle pageSize = document.PageSize;
|
||||
|
||||
if (Title != string.Empty)
|
||||
{
|
||||
if (Title != string.Empty) {
|
||||
pdfContent.BeginText();
|
||||
pdfContent.SetFontAndSize(baseFont, 11);
|
||||
pdfContent.SetRGBColorFill(0, 0, 0);
|
||||
@ -44,8 +40,7 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnEndPage(PdfWriter writer, Document document)
|
||||
{
|
||||
public override void OnEndPage(PdfWriter writer, Document document) {
|
||||
base.OnEndPage(writer, document);
|
||||
|
||||
int pageN = writer.PageNumber;
|
||||
@ -70,8 +65,7 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
pdfContent.EndText();
|
||||
}
|
||||
|
||||
public override void OnCloseDocument(PdfWriter writer, Document document)
|
||||
{
|
||||
public override void OnCloseDocument(PdfWriter writer, Document document) {
|
||||
base.OnCloseDocument(writer, document);
|
||||
|
||||
pageNumberTemplate.BeginText();
|
||||
@ -81,4 +75,5 @@ namespace Fab2ApprovalSystem.PdfGenerator
|
||||
pageNumberTemplate.EndText();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
@ -1,49 +1,44 @@
|
||||
using System.IO;
|
||||
#if !NET8
|
||||
using iTextSharp.text;
|
||||
using iTextSharp.text.html.simpleparser;
|
||||
using iTextSharp.text.pdf;
|
||||
#endif
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace Fab2ApprovalSystem.PdfGenerator
|
||||
{
|
||||
namespace Fab2ApprovalSystem.PdfGenerator;
|
||||
|
||||
/// <summary>
|
||||
/// This class is responsible for rendering a html text string to a PDF document using the html renderer of iTextSharp.
|
||||
/// </summary>
|
||||
public class StandardPdfRenderer {
|
||||
private const int HorizontalMargin = 40;
|
||||
private const int VerticalMargin = 40;
|
||||
|
||||
/// <summary>
|
||||
/// This class is responsible for rendering a html text string to a PDF document using the html renderer of iTextSharp.
|
||||
/// </summary>
|
||||
public class StandardPdfRenderer
|
||||
{
|
||||
private const int HorizontalMargin = 40;
|
||||
private const int VerticalMargin = 40;
|
||||
public byte[] Render(string htmlText, string pageTitle) {
|
||||
byte[] renderedBuffer;
|
||||
|
||||
public byte[] Render(string htmlText, string pageTitle)
|
||||
{
|
||||
byte[] renderedBuffer;
|
||||
|
||||
using (var outputMemoryStream = new MemoryStream())
|
||||
{
|
||||
using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
|
||||
{
|
||||
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
|
||||
pdfWriter.CloseStream = false;
|
||||
pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
|
||||
pdfDocument.Open();
|
||||
using (var htmlViewReader = new StringReader(htmlText))
|
||||
{
|
||||
using (var htmlWorker = new HTMLWorker(pdfDocument))
|
||||
{
|
||||
htmlWorker.Parse(htmlViewReader);
|
||||
}
|
||||
using (MemoryStream outputMemoryStream = new()) {
|
||||
#if !NET8
|
||||
using (Document pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin)) {
|
||||
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
|
||||
pdfWriter.CloseStream = false;
|
||||
pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
|
||||
pdfDocument.Open();
|
||||
using (StringReader htmlViewReader = new StringReader(htmlText)) {
|
||||
using (HTMLWorker htmlWorker = new HTMLWorker(pdfDocument)) {
|
||||
htmlWorker.Parse(htmlViewReader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
renderedBuffer = new byte[outputMemoryStream.Position];
|
||||
outputMemoryStream.Position = 0;
|
||||
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
|
||||
}
|
||||
|
||||
return renderedBuffer;
|
||||
}
|
||||
#endif
|
||||
|
||||
renderedBuffer = new byte[outputMemoryStream.Position];
|
||||
outputMemoryStream.Position = 0;
|
||||
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
|
||||
}
|
||||
|
||||
return renderedBuffer;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user