ASP.NET - generating PDF from asp.net page content

Asked By John Smith on 22-Nov-10 04:04 AM
Hello again,

I need to be able to generate a PDF from the content of an asp.net page, and then email the PDF. I assume the created PDF would first need to be saved prior to being emailed. How best to go about this?

TIA,

John
John Smith replied to Peter Bromberg on 22-Nov-10 09:43 PM
Peter,

Thanks much for the tutorial. It's been very helpful, and I'm up and running with my PDF. Does itextsharp allow for saving PDFs, or directly emailing PDFs?
John Smith replied to Peter Bromberg on 26-Nov-10 11:08 PM
Peter,

I found that if I created a FileStream I could use the byte array already defined (using your code) to save the PDF. For anyone who might need this solution, below is the code I used for the PDF saving portion.

// code snippet with the first two lines taken from Peter's tutorial, illustrating where b and len used in the FileStream came from...
byte[] b = memStream.ToArray();
int len = b.Lenth;

FileStream fs = File.OpenWrite(Server.MapPath("~/myPDFlocation/myPDFname.pdf));
fs.Write(b, 0, len);
fs.Close();

Response.OutputStream.Write(b, 0, len);
Response.OutputStream.Flush();
Response.OutputStream.Close();

// close the previously defined MemoryStream used to output the iTextSharp created PDF to the browser
memStream.Close();