【问题标题】:Getting error when converting to PDF in C# .net web app在 C# .net Web 应用程序中转换为 PDF 时出错
【发布时间】:2011-06-30 00:08:51
【问题描述】:

我正在尝试创建一个将 html 转换为 pdf 文档的应用程序,然后将其作为附件通过电子邮件发送。

我收到此错误The underlying connection was closed: The connection was closed unexpectedly.

尝试了谷歌搜索,但运气不佳;为什么会出现这种情况的任何提示?

编辑: 错误出现在第 28 行:html2pdf.Run(Convert);

<%@ Import Namespace="System.Web.Mail" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Pdfizer" %>
<script language="c#" runat="server">        
    protected override void OnLoad(EventArgs e)
    {    
        sendApplication();    
    }

    private void sendApplication()
    {    
        string template = "X:\\path\\to\\file\\temp.htm"; //HTML Form to send    
        Stream steam = new FileStream("X:\\path\\to\\file\\app.pdf", FileMode.Create, FileAccess.Write, FileShare.Write); //Stream to write to file.
        string fileToSend = "X:\\path\\to\\file\\app.pdf"; //PDF to send(after creation)

        string smtpServer = "spamfilter1.com";
        string sendFrom = "test@test.com";
        string sendTo = "test@test.com";

        //Read template
        StreamReader templateFile = new StreamReader(template);
        string Convert = templateFile.ReadToEnd();
        templateFile.Close();

        //Convert to PDF
        HtmlToPdfConverter html2pdf = new HtmlToPdfConverter();
        html2pdf.Open(steam);
        html2pdf.AddChapter(@"_");
        html2pdf.Run(Convert);
        html2pdf.Close();

        //Send email and attachment
        MailMessage msg = new MailMessage();
        msg.To = sendTo;
        msg.From = sendFrom;
        msg.Subject = "New Application";
        msg.Body = Convert;
        MailAttachment attach = new MailAttachment(fileToSend);
        msg.Attachments.Add(attach);
        SmtpMail.SmtpServer = smtpServer;
        SmtpMail.Send(msg);
        lblStatus.Text = "Application Submitted.";
    }    
</script>   

这是整个 Trace

Server Error in '/WebSite1' Application.
The underlying connection was closed: The connection was closed unexpectedly.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.

Source Error:

Line 26:         html2pdf.Open(steam);
Line 27:         html2pdf.AddChapter(@"_");
Line 28:         html2pdf.Run(Convert);
Line 29:         html2pdf.Close();
Line 30:         


Source File: c:\Users\Jeff\Documents\Visual Studio 2010\WebSites\WebSite1\sstnS.aspx    Line: 28

Stack Trace:

[WebException: The underlying connection was closed: The connection was closed unexpectedly.]
   System.Net.HttpWebRequest.GetResponse() +6111059
   System.Xml.XmlDownloadManager.GetNonFileStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) +107
   System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) +4012381
   System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) +69
   System.Xml.XmlTextReaderImpl.OpenAndPush(Uri uri) +171
   System.Xml.XmlTextReaderImpl.PushExternalEntityOrSubset(String publicId, String systemId, String baseUriStr, Uri& baseUri, String entityName) +252
   System.Xml.XmlTextReaderImpl.DtdParserProxy_PushExternalSubset(String systemId, String publicId) +46
   System.Xml.DtdParserProxy.System.Xml.IDtdParserAdapter.PushExternalSubset(String systemId, String publicId) +16
   System.Xml.DtdParser.ParseExternalSubset() +21
   System.Xml.DtdParser.ParseInDocumentDtd(Boolean saveInternalSubset) +4133593
   System.Xml.DtdParser.Parse(Boolean saveInternalSubset) +54
   System.Xml.DtdParser.System.Xml.IDtdParser.ParseInternalDtd(IDtdParserAdapter adapter, Boolean saveInternalSubset) +31
   System.Xml.XmlTextReaderImpl.ParseDtd() +72
   System.Xml.XmlTextReaderImpl.ParseDoctypeDecl() +219
   System.Xml.XmlTextReaderImpl.ParseDocumentContent() +453
   System.Xml.XmlTextReaderImpl.Read() +145
   System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) +114
   System.Xml.XmlDocument.Load(XmlReader reader) +114
   System.Xml.XmlDocument.LoadXml(String xml) +168
   Pdfizer.HtmlToPdfConverter.Run(String html) +134
   ASP.sstns_aspx.sendApplication() in c:\Users\Jeff\Documents\Visual Studio 2010\WebSites\WebSite1\sstnS.aspx:28
   ASP.sstns_aspx.OnLoad(EventArgs e) in c:\Users\Jeff\Documents\Visual Studio 2010\WebSites\WebSite1\sstnS.aspx:9
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1 

我还应该说电子邮件发送和附件工作正常(已经测试)。

【问题讨论】:

  • 哪一行会抛出这个异常? html2pdf.Run()? SmtpMail.Send()?
  • 您需要隔离问题。该错误表明这是在网络使用时发生的,无需进一步分析,这似乎是通过电子邮件发送时发生的。另外,对未来发展的说明:处理Disposable对象。
  • @Mr.失望我忘了包括跟踪。它发生在 pdf 运行命令中。

标签: c# html pdf


【解决方案1】:

看起来您希望 Run() 方法将数据放入“蒸汽”位置,但它却试图打开一个 URI。 Open() 方法没有告诉它“这里是保存数据的位置”,或者 Run() 方法没有正确保存到该位置。我会先梳理一下这个库的文档,因为我自己并不熟悉它。

【讨论】:

  • 谢谢你让我想到了一些事情。我的模板有: ttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" rel="nofollow" target="_blank">w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> w3.org/1999/xhtml"> 这是从网站上拉出来的。刚改成
【解决方案2】:

html2pdf 是否调用了网络服务?还是自给自足?

【讨论】:

    【解决方案3】:

    您能否发布包括堆栈跟踪在内的整个错误消息?我怀疑当您尝试发送电子邮件时发生了错误。堆栈跟踪将验证这一点。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2011-09-17
      • 2021-06-28
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多