58 lines
1.6 KiB
C#
58 lines
1.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using HiQPdf;
|
|
|
|
namespace EnVisage.Code
|
|
{
|
|
public class PDFExporter
|
|
{
|
|
#region fields and properties
|
|
private HtmlToPdf htmlToPdf;
|
|
#endregion
|
|
|
|
#region public methods
|
|
/// <summary>
|
|
/// Public constructor. Initializes PDF Converter
|
|
/// </summary>
|
|
public PDFExporter()
|
|
{
|
|
htmlToPdf = new HtmlToPdf();
|
|
htmlToPdf.SerialNumber = System.Configuration.ConfigurationManager.AppSettings["HiQPdfSerialNumber"];
|
|
int browserWidth = 1920;
|
|
int.TryParse(System.Configuration.ConfigurationManager.AppSettings["HiQPdfBrowserWidth"], out browserWidth);
|
|
htmlToPdf.BrowserWidth = browserWidth;
|
|
htmlToPdf.Document.PageSize = PdfPageSize.A4;
|
|
htmlToPdf.Document.PageOrientation = PdfPageOrientation.Landscape;
|
|
htmlToPdf.Document.PdfStandard = PdfStandard.Pdf;
|
|
htmlToPdf.Document.Margins = new PdfMargins(5);
|
|
htmlToPdf.Document.FontEmbedding = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Waits delaySeconds seconds and then parses the page at url authorized by given cookies collection and returns a byte array to be saved to file
|
|
/// </summary>
|
|
public byte[] ExportPage(string url, int delaySeconds, bool usePrintStylesheet, HttpCookieCollection cookies)
|
|
{
|
|
byte[] pdfBuffer = null;
|
|
|
|
htmlToPdf.TriggerMode = ConversionTriggerMode.WaitTime;
|
|
htmlToPdf.WaitBeforeConvert = delaySeconds;
|
|
|
|
if (usePrintStylesheet)
|
|
htmlToPdf.MediaType = "print";
|
|
|
|
foreach (string key in cookies.Keys)
|
|
{
|
|
htmlToPdf.HttpCookies.AddCookie(cookies[key].Name, cookies[key].Value);
|
|
}
|
|
|
|
pdfBuffer = htmlToPdf.ConvertUrlToMemory(url);
|
|
|
|
return pdfBuffer;
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
} |