0

When executing the following code no errors are thrown but the pdf generated as an attachment is blank and its file size is 0k.

Edit:

Dim page As Page = New Page()
Dim form As HtmlForm = New HtmlForm()
page.Controls.Add(form)

Dim bpa As UserControl_BpaReceipt = page.LoadControl("~/UserControl/BpaReceipt.ascx")
bpa._thisCart = _thisCart
form.Controls.Add(bpa)
Dim writer As StringWriter = New StringWriter()

Me.Server.Execute(page, writer, False)

End Edit:

Dim bytes = Encoding.UTF8.GetBytes(writer.ToString())

Dim input As MemoryStream = New MemoryStream(bytes)

Dim output As MemoryStream = New MemoryStream()
''//Dim reader As TextReader = New StringReader(writer.ToString())

Dim doc As Document = New Document(PageSize.A4.Rotate, 30, 30, 30, 30)

Dim pdfWriter As PdfWriter = pdfWriter.GetInstance(doc, output)

doc.Open()

XMLWorkerHelper.GetInstance().ParseXHtml(pdfWriter, doc, input, Nothing)

email.Attachments.Add(New Attachment(output, "Quote.pdf", "application/pdf"))

Dim smtp As New SmtpClient()
smtp.Credentials = New System.Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPUser").ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPPassword").ToString())
smtp.Send(email)
doc.Close()

I am very new to this process and any help would be greatly appreciated. Also, to clarify if I output to a file instead of a stream the html in the writer.toString() is outputted as expected.

I appreciate any thoughts on the matter.

Thanks,

Edit:

Dim page As Page = New Page()
Dim form As HtmlForm = New HtmlForm()
page.Controls.Add(form)

Dim bpa As UserControl_BpaReceipt = page.LoadControl("~/UserControl/BpaReceipt.ascx")
bpa._thisCart = _thisCart
form.Controls.Add(bpa)
Dim writer As StringWriter = New StringWriter()

Me.Server.Execute(page, writer, False)

email.IsBodyHtml = True


If (toValues.Value.Contains(";")) Then
    Dim toArray = toValues.Value.Split(";")
    For Each value In toArray
        email.To.Add(value)
    Next
Else
    email.To.Add(toValues.Value.ToString())
End If

email.Subject = txtSubject.Text
email.Body = "<br /><br /><br />" & writer.ToString()

email.From = New MailAddress("webadmin@managemobility.com")

Dim bytes = Encoding.UTF8.GetBytes(writer.ToString())

Dim input As MemoryStream = New MemoryStream(bytes)

Dim output As MemoryStream = New MemoryStream()
''//Dim reader As TextReader = New StringReader(writer.ToString())

Dim doc As Document = New Document(PageSize.A4.Rotate, 30, 30, 30, 30)

Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(doc, output)
pdfWrite.CloseStream = False
doc.Open()

XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, doc, input, Nothing)
doc.Close()
email.Attachments.Add(New Attachment(output, "Quote.pdf", "application/pdf"))

Dim smtp As New SmtpClient()
smtp.Credentials = New System.Net.NetworkCredential(System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPUser").ToString(), System.Web.Configuration.WebConfigurationManager.AppSettings("SMTPPassword").ToString())
smtp.Send(email)
''//doc.Close()

I edited the the code to reflect your suggestions and I'm afraid I still received the same output an empty pdf 0kb. Thank you so much for your suggestions I to am quite vexed by this issue also and truly appreciate the suggestions.

Chris Haas
  • 53,986
  • 12
  • 141
  • 274
imaginationpluscode
  • 137
  • 1
  • 2
  • 17
  • Can you add the definition of writer; the problem is almost certainly writer.tostring() – competent_tech Oct 28 '13 at 20:14
  • I have added the definition and use of the writer. PLease note that in debug I can see the output of the writer when hovering over writer.toString(). Thanks again!!!!!! – imaginationpluscode Oct 28 '13 at 20:19
  • Also, when I set email.Body = writer.toString() I receive the expected output. – imaginationpluscode Oct 28 '13 at 20:21
  • 1
    You use output before `doc.Close`. But the document is not fully created before closing `doc`. Thus, it's not surprising you have issues reading that document. Cf. [This answer](http://stackoverflow.com/questions/19618756/memorystream-looks-like-corrupt-the-file-using-itextsharp/19619196#19619196). – mkl Oct 28 '13 at 22:20
  • I edited the the code to reflect your suggestions and I'm afraid I still received the same output an empty pdf 0kb. Thank you so much for your suggestions I to am quite vexed by this issue also and truly appreciate the suggestions. – imaginationpluscode Oct 29 '13 at 01:47
  • 2
    Can you provide the pdf result for inspection? And have you tried not only mailing the pdf but at the same time storing the memory stream to file? If the pdf stored as file and the one mailed differ, you know you have a mailing issue. If they don't, you can be sure you have a pdf creation issue. Currently you have too many different aspects potentially relevant for the problem. – mkl Oct 29 '13 at 06:10
  • 1
    I agree with @mkl, stop trying to email the file. For now, just try making the PDF by writing it to disk. Once you've got that down you can then move on to emailing it. You're juggling too many different things. – Chris Haas Oct 29 '13 at 13:14
  • Thank you I will try this as soon as I get a free moment today!! – imaginationpluscode Oct 31 '13 at 12:26

1 Answers1

0

As suggested in the comments, make sure you can save the file to disk before you start trying to email it. Once you can do that then move on to attaching it and sending email. Below is a code snippet of what I have used before to attach a pdf to an email using the System.Net.Mail class. (it's c#, but you get the idea)

 output.Seek(0, SeekOrigin.Begin);
 System.Net.Mime.ContentType contentType = new System.Net.Mime.ContentType();
 contentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
 contentType.Name = "Quote.pdf";

 Attachment attachment = new Attachment(output, contentType);
 mail.Attachments.Add(attachment);
ovaltein
  • 1,185
  • 2
  • 12
  • 34