Welcome Guest Search | Active Topics | Sign In | Register

EO.PDF for .NET - ConvertHtml to Stream Options
Chris Hey
Posted: Monday, October 24, 2011 8:01:48 AM
Rank: Newbie
Groups: Member

Joined: 10/24/2011
Posts: 3
Hi,

I am evaluating your product for a project i am working on and i have successfully used the HtmlToPdf.ConvertHtml method to return a PDF to the browser via the response output stream.

I am also trying to use the same method to save convert the HTML to a stream and attach it to an email. Please find a snippet of code below:

MemoryStream pdfStream = new MemoryStream();
HtmlToPdf.ConvertHtml(TextBoxEOContent.Text, pdfStream);
MailMessage email = new MailMessage("test@html2pdf.net", "chris.hey@nepcco.co.uk");
email.Subject = "Testing Essential Objects HTML2PDF";
ContentType contentType = new ContentType();
contentType.MediaType = MediaTypeNames.Application.Pdf;
contentType.Name = "Test PDF.pdf";
email.Attachments.Add(new Attachment(pdfStream, contentType));
SmtpClient client = new SmtpClient("localhost");
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = true;
client.Send(email);

The email comes through with a file attached as a PDF, however i cannot open it due it being being in the correct format. I have tried changing the content type with no resolution. I have also tried taking the stream output from the ConvertHtml method and passing it into a filestream to write a file to disk, which also doesn't work. It appears that the stream output is fine when writing to the response but not to a MemoryStream.

Any ideas?

Thanks

Chris Hey
eo_support
Posted: Monday, October 24, 2011 8:36:12 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi Chris,

Have you tried using a new memory stream?

byte[] pdfFileBytes = pdfStream.ToArray();
email.Attachments.Add(new Attachment(new MemoryStream(pdfFileBytes), contentType));

The PDF converter does not care what type of stream you use --- it's all just a sequence of bytes. So there is no reason response out stream would work but MemoryStream wouldn't. They are absolutely identical from the converter point of view.

Thanks!
Chris Hey
Posted: Monday, October 24, 2011 8:50:50 AM
Rank: Newbie
Groups: Member

Joined: 10/24/2011
Posts: 3
Brilliant!

Creating a new stream from the byte array worked, any idea why that would solve it? I am a curious dev and also like to learn lessons so i don't hit the same gotcha more than once.

Thanks again.

Chris
eo_support
Posted: Monday, October 24, 2011 9:09:33 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

That's because the two streams' "internal state" are different. Specifically, the first stream's current position is at the end of the stream, while the second stream's current position pointer is at the beginning of stream.

Thanks
Chris Hey
Posted: Monday, October 24, 2011 9:18:04 AM
Rank: Newbie
Groups: Member

Joined: 10/24/2011
Posts: 3
Just set the position of the first stream to zero, used that to create the attachment and that works too. Can't believe i didn't think of it myself!

Might have to blog/post on Code Project so others can find the solution.

Thanks
eo_support
Posted: Monday, October 24, 2011 9:22:05 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Glad that worked for you too. Please remember to link to us whenever you blog about this. : )
moremotivated
Posted: Monday, March 12, 2012 9:35:52 PM
Rank: Newbie
Groups: Member

Joined: 3/12/2012
Posts: 2
Chris Hey wrote:
Just set the position of the first stream to zero, used that to create the attachment and that works too. Can't believe i didn't think of it myself!

Might have to blog/post on Code Project so others can find the solution.

Thanks


Thanks to both of you for pointing me in the right direction and also explaining the reasoning behind this with the stream positions :)

Great product I will be purchasing for our business!
eo_support
Posted: Tuesday, March 13, 2012 9:24:14 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
moremotivated wrote:

Thanks to both of you for pointing me in the right direction and also explaining the reasoning behind this with the stream positions :)

Great product I will be purchasing for our business!


You are welcome. Please feel free to let us know if you have any questions.


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.