Mike Phares 7650bf2869 Removed PdfViewController, HtmlViewRenderer and FakeView to be replaced with ViewEngineResult Render method
Added HttpException class for missing HttpException for net8

Wrapped HttpContext.Session, GetJsonResult, IsAjaxRequest and GetUserIdentityName in controllers for net8

Added AuthenticationService to test Fab2ApprovalMKLink code for net8

Compile conditionally flags to debug in dotnet core
2025-05-23 12:51:42 -07:00

51 lines
1.8 KiB
C#

#if !NET8
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
#endif
using System.IO;
namespace Fab2ApprovalSystem.PdfGenerator;
public class StandardPdfRenderer {
private const int HorizontalMargin = 40;
private const int VerticalMargin = 40;
public static byte[] GetPortableDocumentFormatBytes(string pageTitle, string htmlText) {
byte[] results;
MemoryStream memoryStream = GetPortableDocumentFormat(pageTitle, htmlText);
results = new byte[memoryStream.Position];
memoryStream.Position = 0;
memoryStream.Read(results, 0, results.Length);
return results;
}
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();
#if !NET8
using (Document pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin)) {
PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, result);
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);
}
}
}
#endif
return result;
}
}