从Memory Stream创建并附加PDF文件 [英] Create and Attach PDF file from Memory Stream
问题描述
我必须将PDF文件作为附件发送,我有一个带有一些文本的HTML文件,我在运行时将数据库中的值添加到此文件中,就像这样
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);
现在我必须生成PDF文件并在运行时将其作为附件发送,我无法在物理上保存pdf文件。这就是我试过的内容
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(readFile);
然后像这样附上它
系统。 Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, file.pdf跨度>);
但是当我把它作为附件发送时,它没有打开我在电子邮件中收到的文件。我也尝试了这个
mailMessage.Attachments.Add(System.Net.Mail.Attachment.CreateAttachmentFromString(readFile, application / pdf));
mailMessage.Attachments.Last()。ContentDisposition.FileName = myFile.pdf;
我也试过这个
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 个字节;
}
但是我在电子邮件中收到它后没有打开pdf文件。
如何生成和发送PDF文件运行时?
Garth是正确的,你无法打开PDF文件的原因是因为它不是一个有效的文件。您将readFile
中的HTML文本写入ms
流,然后将该流的内容附加为 file.pdf 。
您需要将HTML格式转换为PDF格式。尝试google-ing,这是一个有点常见的任务,你可以找到相当多的解决方案。
例如这里有一个解决方案(它使用iTextSharp):
http://stackoverflow.com/questions/2822843/itextsharp-html- to-pdf#answer-30555304 [ ^ ]
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
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
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
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
mailMessage.Attachments.Add(System.Net.Mail.Attachment.CreateAttachmentFromString(readFile, "application/pdf"));
mailMessage.Attachments.Last().ContentDisposition.FileName = "myFile.pdf";
I have tried this too
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?
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 fromreadFile
into ams
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[^]
这篇关于从Memory Stream创建并附加PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!