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
44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
#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;
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif
|
|
|
|
renderedBuffer = new byte[outputMemoryStream.Position];
|
|
outputMemoryStream.Position = 0;
|
|
outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
|
|
}
|
|
|
|
return renderedBuffer;
|
|
}
|
|
} |