ASP.NET - generating PDF from 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 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?


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

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

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);

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

// close the previously defined MemoryStream used to output the iTextSharp created PDF to the browser