Skip to content

Instantly share code, notes, and snippets.

@jackinf
Created April 17, 2015 13:01
Show Gist options
  • Save jackinf/ed5d2d37596be26ea8f2 to your computer and use it in GitHub Desktop.
Save jackinf/ed5d2d37596be26ea8f2 to your computer and use it in GitHub Desktop.
[C#] [PDF] EO.PDF generation controller
public partial class PdfController : LocalDefaultController
{
public void GeneratePdfFromUrl()
{
// Option 1: save pdf file as...
var options = new HtmlToPdfOptions();
HttpCookieCollection oCookies = Request.Cookies;
for (int j = 0; j < oCookies.Count - 1; j++)
{
// Convert between the System.Net.Cookie to a System.Web.HttpCookie...
HttpCookie oCookie = oCookies.Get(j);
var oC = new Cookie
{
Domain = oCookie.Domain,
Expires = oCookie.Expires,
Name = oCookie.Name,
Path = oCookie.Path,
Secure = oCookie.Secure,
Value = oCookie.Value
};
options.Cookies.Add(oC);
}
//options.InvisibleElementIds = "sidebar;navBarTop;footer";
options.PageSize = PdfPageSizes.A4;
options.OutputArea = new RectangleF(0.5F, 0.5F, 7.5F, 10F);
options.AdditionalHeaders = new[]
{
"X-Mode-Printing: true",
};
//HtmlToPdf.ConvertUrl(url, @"C:\Users\username\Desktop\TestConvertBBig.pdf", options);
var test = "<div id='ShellForm' style='... ds, </div>";
using (var stream = new MemoryStream())
{
//const string url = @"http://www.facebook.com";
//HtmlToPdf.ConvertUrl(url, stream);
HtmlToPdf.ConvertHtml(test, stream);
ControllerContext.HttpContext.Response.Clear();
ControllerContext.HttpContext.Response.ContentType = "application/pdf";
ControllerContext.HttpContext.Response.BinaryWrite(stream.ToArray());
ControllerContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMdd") + ".pdf");
ControllerContext.HttpContext.Response.Flush();
ControllerContext.HttpContext.Response.End();
}
// Option 2: Send PDF as email attachement
var memoryStream = new MemoryStream();
//const string url2 = @"http://www.google.com";
//HtmlToPdf.ConvertUrl(url2, memoryStream);
HtmlToPdf.ConvertHtml(test, memoryStream);
//var contentType = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
memoryStream.Seek(0, SeekOrigin.Begin);
var attach = new Attachment(memoryStream, "theFile.pdf", "application/pdf");
//attach.ContentDisposition.FileName = "myFile.pdf";
// I guess you know how to send email with an attachment
var credential = new NetworkCredential("[email protected]", "xxx");
var smtpServer = new SmtpClient("smtp.gmail.com")
{
Host = "smtp.gmail.com",
Port = 587,
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
UseDefaultCredentials = true,
Credentials = credential
};
var mail = new MailMessage { From = new MailAddress("[email protected]"), Subject = "you subject", Body = "your description", Priority = MailPriority.High };
mail.To.Add("[email protected]");
mail.Attachments.Add(attach);
smtpServer.Send(mail);
memoryStream.Flush();
memoryStream.Close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment