0

执行以下代码时不会抛出任何错误,但作为附件生成的 pdf 为空白且文件大小为 0k。

编辑:

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)

结束编辑:

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()

我对这个过程很陌生,任何帮助都将不胜感激。此外,为了澄清我是否输出到文件而不是流, writer.toString() 中的 html 按预期输出。

我很欣赏对此事的任何想法。

谢谢,

编辑:

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()

我编辑了代码以反映您的建议,恐怕我仍然收到相同的输出一个空的 pdf 0kb。非常感谢您的建议,我也对这个问题感到非常恼火,并且非常感谢您的建议。

4

1 回答 1

0

正如评论中所建议的,在开始尝试通过电子邮件发送文件之前,请确保您可以将文件保存到磁盘。一旦你可以做到这一点,然后继续附加它并发送电子邮件。下面是我之前使用 System.Net.Mail 类将 pdf 附加到电子邮件的代码片段。(它是 c#,但你明白了)

 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);
于 2013-10-30T20:55:31.523 回答