执行以下代码时不会抛出任何错误,但作为附件生成的 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。非常感谢您的建议,我也对这个问题感到非常恼火,并且非常感谢您的建议。