-3

I have a web form with a grid.I want to generate a pdf of the whole web form and grid data,save it on the server side itself,without saving it on the local machine as we do while exporting data to pdf, and send that pdf as an email attachment.All these functionalities i want to perform while the website is live on server.How can i achieve this task.

I have the code to convert grid data to pdf,but dont know how to save that pdf file on server

i m using following type of code for converting to pdf

private void Export_to_Pdf()

{

Response.ContentType = “application/pdf”;
Response.AddHeader(“content-disposition”, “attachment;filename=DotnetGeekBlog.pdf”);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string str = “<h1 title=’Header’ align=’Center’> Writing To PDF Using ASP.NET</h1> <br><table align=’Center’><tr><td style=’width:100px;color:green’> <b>iTextSharp</b></td><td style=’width:100px;color:red’>dotnetgeekblog</td></tr></table>”;
StringReader sr = new StringReader(str);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

}

the file just gets downloaded on the local machine.i want to save it on server and send it in email.

Thanks in advance.

smita kapse
  • 31
  • 3
  • 11
  • If you are not saving it how can you email it as attachment – Vinay Pratap Singh Bhadauria Oct 17 '13 at 09:37
  • Check this question: http://stackoverflow.com/questions/10706549/how-to-create-pdf-in-asp-net –  Oct 17 '13 at 09:37
  • you have to save this pdf on server side and delete or remove after mail send...and about you download this may depend on your code you can remove download code...it work for me – Prafulla Oct 17 '13 at 09:38
  • We used the 3rd-party library http://www.dynamicpdf.com/ (dynamic pdf generator) to create pdfs on our website. It was easy to work with, but it is not free. – mortb Oct 17 '13 at 09:43
  • @REx - You can attach using streams. A memory stream could also serve. http://msdn.microsoft.com/en-us/library/6sdktyws.aspx – Kami Oct 17 '13 at 09:56

1 Answers1

0

You can use System.net namespace as follows:

public static void CreateMessageWithAttachment(string server)
    {
        MemoryStream file = // Your Memeory stream to send as attachment
        MailMessage message = new MailMessage(
           "abc@xyz.com",
           "pqr@xyz.com",
           "report.",
           "See the attached pdf.");

        Attachment data = new Attachment( file , "example.pdf", "application/pdf" );

        message.Attachments.Add(data);

        //Send the message.
        SmtpClient client = new SmtpClient(server);
        // Add credentials if the SMTP server requires them.
        client.Credentials = CredentialCache.DefaultNetworkCredentials;

          client.Send(message);

        }
Jatin patil
  • 4,252
  • 1
  • 18
  • 27