Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to send PDF file as attachement, I am having an HTML file with some text in it and I am adding values from database to this file at runtime, Like this

C#
  var path = Server.MapPath("~/StaticFile/UserDetails.htm");
        string readFile = System.IO.File.ReadAllText(path);
readFile.Replace("%Name%",username_from_db);
readFile.Replace("%Age%",age_from_db);


Now I have to generate PDF file and send it as attachement on runtime,I cant save the pdf file physically.This is what I tried
C#
System.IO.MemoryStream ms = new System.IO.MemoryStream();
               System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
              writer.Write(readFile);


then attaching it like this
C#
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, "file.pdf");

but when I send it as attachement, it is not opening the file which I am getting in email. I also tried this
C#
mailMessage.Attachments.Add(System.Net.Mail.Attachment.CreateAttachmentFromString(readFile, "application/pdf"));
              mailMessage.Attachments.Last().ContentDisposition.FileName = "myFile.pdf";

I have tried this too
C#
 var byts = GetBytes(readFile);
System.IO.MemoryStream ms = new System.IO.MemoryStream(byts);
  var att = new System.Net.Mail.Attachment(ms, "MyFile.pdf");
mailMessage.Attachments.Add(att);

  private static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }

But its not opening the pdf file after I recieve it in email.
How can I generate and send PDF on runtime?
Posted
Updated 10-Oct-15 21:03pm
v4
Comments
Garth J Lancaster 11-Oct-15 2:08am    
I dont see where you are actually generating the pdf from the html file - we cant see your screen/code, so you really need to provide all relevant information - please update your question.

One suggestion - generate the pdf to a file, put a breakpoint after that, see if the pdf can be opened from explorer or Acrobat if you have it (or any other pdf viewer tool) - if it cant/the file is empty, thats an issue to fix first before you attempt to send it via email
Syed Salman Raza Zaidi 11-Oct-15 2:51am    
Please see the updated question

1 solution

Garth is correct, the reason why you're unable to open your "PDF" file is because it's not a valid file. You wrote a HTML text from readFile into a ms stream and then attached that stream's content as file.pdf.

You'll need to convert the HTML format into a PDF format. Try google-ing for this, this is a somewhat common task and you can find quite a few solutions to it.
For example here is one of those solutions (it uses iTextSharp):
http://stackoverflow.com/questions/2822843/itextsharp-html-to-pdf#answer-30555304[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900