Merged PR 17498: Removed PdfViewController, HtmlViewRenderer and FakeView to be replaced with...

Removed PdfViewController, HtmlViewRenderer and FakeView to be replaced with ViewEngineResult Render method

To test use url
http://mestsa05ec.infineon.com:8080/ecn/printecnpdf?ecnNumber=82654
This commit is contained in:
2025-05-28 22:21:03 +02:00
parent 8bae94de96
commit 65a433e9ab
8 changed files with 571 additions and 683 deletions

View File

@ -1,28 +0,0 @@
#if !NET8
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FakeView.cs" company="SemanticArchitecture">
// http://www.SemanticArchitecture.net pkalkie@gmail.com
// </copyright>
// <summary>
// Defines the FakeView type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Fab2ApprovalSystem.PdfGenerator {
using System;
using System.IO;
using System.Web.Mvc;
public class FakeView : IView {
#region IView Members
public void Render(ViewContext viewContext, TextWriter writer) {
throw new NotImplementedException();
}
#endregion
}
}
#endif

View File

@ -1,49 +0,0 @@
#if !NET8
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HtmlViewRenderer.cs" company="SemanticArchitecture">
// http://www.SemanticArchitecture.net pkalkie@gmail.com
// </copyright>
// <summary>
// This class is responsible for rendering a HTML view to a string.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Fab2ApprovalSystem.PdfGenerator {
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
/// <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) {
var renderedView = new StringBuilder();
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);
var oldContext = HttpContext.Current;
HttpContext.Current = fakeContext;
using (var viewPage = new ViewPage()) {
var html = new HtmlHelper(CreateViewContext(responseWriter, fakeControllerContext), viewPage);
html.RenderPartial(viewName, viewData);
HttpContext.Current = oldContext;
}
}
return renderedView.ToString();
}
private static ViewContext CreateViewContext(TextWriter responseWriter, ControllerContext fakeControllerContext) {
return new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), responseWriter);
}
}
}
#endif

View File

@ -1,54 +0,0 @@
#if !NET8
// --------------------------------------------------------------------------------------------------------------------
// <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();
}
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");
}
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);
}
}
}
}
#endif

View File

@ -1,27 +1,38 @@
#if !NET8
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
#endif
using System.IO;
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;
public byte[] Render(string htmlText, string pageTitle) {
byte[] renderedBuffer;
public static byte[] GetPortableDocumentFormatBytes(string pageTitle, string htmlText) {
byte[] results;
using (MemoryStream memoryStream = GetPortableDocumentFormat(pageTitle, htmlText)) {
results = new byte[memoryStream.Position];
memoryStream.Position = 0;
memoryStream.Read(results, 0, results.Length);
}
return results;
}
using (MemoryStream outputMemoryStream = new()) {
#if !NET8
using (Document pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin)) {
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
public static void WritePortableDocumentFormatToFile(string pageTitle, string htmlText, string path) {
using (MemoryStream memoryStream = GetPortableDocumentFormat(pageTitle, htmlText)) {
using (FileStream fileStream = new(path, FileMode.Create)) {
memoryStream.CopyTo(fileStream);
}
}
}
public static MemoryStream GetPortableDocumentFormat(string pageTitle, string htmlText) {
MemoryStream result = new();
using (Document pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin)) {
using (PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, result)) {
pdfWriter.CloseStream = false;
pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
pdfDocument.Open();
@ -30,15 +41,9 @@ public class StandardPdfRenderer {
htmlWorker.Parse(htmlViewReader);
}
}
}
#endif
renderedBuffer = new byte[outputMemoryStream.Position];
outputMemoryStream.Position = 0;
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
}
return renderedBuffer;
return result;
}
}