"该文件没有打开"错误只在生产与iTextSharp的 [英] "The document is not open" error only in production with iTextSharp

查看:222
本文介绍了"该文件没有打开"错误只在生产与iTextSharp的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到的一个iTextSharp的文件未打开错误,但只有在生产。代码运行我的开发机器上,并在分期罚款。我在阶段服务器上的Temp文件夹设置相同的权限。

I am getting a "The document is not open" error in iTextSharp, but only in production. The code runs fine on my dev machine and in staging. I have the same permissions set in the Temp folder on the stage server.

public static byte[] ConvertHtmlToPdf(string html)
    {
        html = HtmlPostProcessor.Process(html);
        byte[] fileData = null;
        string tempPath = ConfigurationManager.AppSettings["TempDirectory"];
        string tempPDFFile = Path.Combine(tempPath, Guid.NewGuid() + ".pdf");
        int num = FontFactory.RegisterDirectory(@"C:\Windows\Fonts");

        using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))
        {
            using (Document document = new Document(PageSize.LETTER, 50, 50, 50, 50))
            {
                document.Open();
                PdfWriter.GetInstance(document, fs);
                using (StringReader stringReader = new StringReader(html))
                {

                    List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null);
                    foreach (IElement item in parsedList)
                    {
                        document.Add(item);
                    }
                }
            }
        }

        FileStream generatedPDF = File.Open(tempPDFFile, FileMode.Open);
        fileData = new byte[(int)generatedPDF.Length]; 
        int result = generatedPDF.Read(fileData, 0, (int)generatedPDF.Length);

        generatedPDF.Close();

        File.Delete(tempPDFFile);

        return fileData;
    }

一个PDF文件无法生成,所以我知道它运行过去

A pdf file does get created, so I know it runs past

using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))

最起码。

这个代码运行在开发和分期得很好,但它抛出一个错误在生产中。 ?有什么想法,为什么这可能是

This code runs just fine in dev and staging, but it throws an error in production. Any thoughts as to why that could be?

推荐答案

使用对提高代码kuujinbo的建议,现在看起来是这样的:

Using kuujinbo's suggestions about improving the code, it now looks like this:

    public static byte[] ConvertHtmlToPdf(string html)
    {
        html = HtmlPostProcessor.Process(html);
        byte[] fileData = null;
        int num = FontFactory.RegisterDirectory(@"C:\Windows\Fonts");

        using (MemoryStream ms = new MemoryStream(html.Length))
        {
            using (Document document = new Document(PageSize.LETTER, 50, 50, 50, 50))
            {
                PdfWriter.GetInstance(document, ms);
                using (StringReader stringReader = new StringReader(html))
                {

                    List<IElement> parsedList = HTMLWorker.ParseToList(stringReader, null);
                    document.Open();
                    foreach (IElement item in parsedList)
                    {
                        document.Add(item);
                    }
                }
            }

            fileData = ms.ToArray();
        }
        return fileData;
    }

的问题是,里面:

using (Document document = new Document(PageSize.LETTER, 50, 50, 50, 50))

语句,另一个异常被抛出,在我的情况是:

statement, another exception was being thrown, in my case it was:

WebException: Unable to connect to the remote server

这发生,因为图像我是用了HTML文件,正在被转换为PDF中有联系,被指向托管在同一服务器上其他网站。服务器有内部和外部的IP地址,但我忘了编辑的主机服务器上的文件,以便使用这些DNS名称将使用内部地址,而不是在外部的重定向到自己。

which happened because an image I was using that was linked inside the HTML document, that was being converted to a PDF, was pointing to another website hosted on the same server. The server had internal and external IP addresses, but I forgot to edit the hosts file on the server so that redirects to itself using those DNS names would use internal addresses instead of the external ones.

异常消息是该文档无法打开的原因,是因为(FYI:我在这里假设),因为我是在一个使用块,基本上充当终于的声明,任何时候有一个在iText库异常文档关闭,而当使用试图调用Dispose(这是在堆栈跟踪露面)iText的错误,因为Document对象必须已经被关闭。

The reason the Exception message was "The document is not open", is because (FYI: I'm assuming here) since I was in a using block that basically acts as a "finally" statement, anytime there is an exception in the iText library the Document closes, and when the using tries to call Dispose (which is what showed up in the stack trace) iText errors out because the Document object must already be closed.

这篇关于&QUOT;该文件没有打开&QUOT;错误只在生产与iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆