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
49 lines
1.8 KiB
C#
49 lines
1.8 KiB
C#
using iTextSharp.text;
|
|
using iTextSharp.text.html.simpleparser;
|
|
using iTextSharp.text.pdf;
|
|
|
|
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;
|
|
using (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();
|
|
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();
|
|
using (StringReader htmlViewReader = new StringReader(htmlText)) {
|
|
using (HTMLWorker htmlWorker = new HTMLWorker(pdfDocument)) {
|
|
htmlWorker.Parse(htmlViewReader);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} |