为什么在c#中将HTML文件转换为PDF时会超时?

本文关键字:PDF 超时 转换 中将 HTML 文件 为什么 | 更新日期: 2023-09-27 18:01:50

我有以下代码:

 public static void WriteHTML(string cFile, List<Movie> mList)
        {
            int lineID = 0;
            string strMovie, strGenre, tmpGenre = null;
            // initiates streamwriter for catalog output file
            FileStream fs = new FileStream(cFile, FileMode.Create);
            StreamWriter catalog = new StreamWriter(fs);
            string strHeader = "<style type='"text/css'">'r'n" + "<!--'r'n" + "tr#odd {'r'n" + "    background-color:#e2e2e2;'r'n" + "  vertical-align:top;'r'n" + "}'r'n" + "'r'n" + "tr#even {'r'n" + "   vertical-align:top;'r'n" + "}'r'n" + "div#title {'r'n" + "  font-size:16px;'r'n" + "    font-weight:bold;'r'n" + "}'r'n" + "'r'n" + "div#mpaa {'r'n" + "    font-size:10px;'r'n" + "}'r'n" + "'r'n" + "div#genre {'r'n" + " font-size:12px;'r'n" + "    font-style:italic;'r'n" + "}'r'n" + "'r'n" + "div#plot {'r'n" + "   height: 63px;'r'n" + "  font-size:12px;'r'n" + "    overflow:hidden;'r'n" + "}'r'n" + "-->'r'n" + "</style>'r'n" + "'r'n" + "<html>'r'n" + "    <body>'r'n" + "     <table>'r'n";
            catalog.WriteLine(strHeader);
            foreach (Movie m in mList)
            {
                strMovie = lineID == 0 ? "          <tr id='"odd'" style='"page-break-inside:avoid'">" : "          <tr id='"even'" style='"page-break-inside:avoid'">";
                catalog.WriteLine(strMovie);
                foreach (string genre in m.Genres)
                    tmpGenre += ", " + genre;
                try
                { strGenre = tmpGenre.Substring(2); }
                catch (Exception)
                { strGenre = null; }
                strMovie = "                <td>'r'n" + "                   <img src='".''images''" + m.ImageFile + "'" width='"75'" height='"110'">'r'n" + "               </td>'r'n" + "              <td>'r'n" + "                   <div id='"title'">" + m.Title + "</div>'r'n" + "                    <div id='"mpaa'">" + m.Certification + " " + m.MPAA + "</div>'r'n" + "                  <div id='"genre'">" + strGenre + "</div>'r'n" + "                   <div id='"plot'">" + m.Plot + "</div>'r'n" + "              </td>'r'n" + "          </tr>'r'n";
                catalog.WriteLine(strMovie);
                lineID = lineID == 0 ? 1 : 0;
            }
            catalog.WriteLine("     </table>'r'n" + "   </body>'r'n" + "</html>");
            catalog.Close();
        }


        public static void WritePDF(string cFile, string pdfFile)
        {
            // Sets up PDF to write to
            EO.Pdf.HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f);
            EO.Pdf.HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, .25f, 7.5f, 10.25f);
            HtmlToPdf.ConvertUrl(cFile, pdfFile);
        }

我的HTML文件写得很好,但是当它试图将HTML文件转换为PDF时,我得到一个异常,它超时了。

我之前做了一个测试,并让它在WriteHTML函数中转换代码(而不是文件),它工作得很好。我已经确认cFile存在并且是一个有效的文件(先前在WriteHTML中创建)。pdfFile的路径是有效的,并且文档没有说明文件需要已经存在。ConvertHTML不需要现有文件)。

我唯一能想到的是catalog.html文件还没有发布,还没有准备好阅读。我确保在WriteHTML函数中关闭了它。我如何测试文件是否可以被读取?

尝试设置。maxloadwaittime = 120000,但没有成功。

任何线索将不胜感激!

为什么在c#中将HTML文件转换为PDF时会超时?

经过一系列的进一步测试,并浏览了EO支持论坛,这似乎是EO免费版本的一个限制。对于大于3MB的HTML文件,它似乎有困难。

这是一个耻辱,因为EO产品是非常好的,但不幸的是不值得250美元的IMO。