"Document is not open" error only during iTextSharp production

I get a "Document not Open" error in iTextSharp, but only in production. The code works fine on my dev machine and is underway. I have the same permissions set in the Temp folder on the scene 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; } 

The pdf file is being created, so I know that it passed by

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

at least.

This code works fine in dev and creates, but it throws a production error. Any thoughts on why this might be?

+3
source share
2 answers

Using kuujinbo's suggestions for 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; } 

The problem was that inside:

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

another exception was thrown, in my case it was:

 WebException: Unable to connect to the remote server 

which happened because the image I used was linked inside an HTML document that was converted to PDF, 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 redirecting to itself using these DNS names will use internal addresses instead of external ones.

The reason for the exception message is: β€œThe document is not open” because (FYI: I assume here), since I was in the block, which basically acts as the β€œfinal” statement, at any time there is an exception in the iText library the document is closed, and when an attempt tries to call Dispose (which was detected in the stack trace), iText throws an error because the Document object should already be closed.

+3
source

Didn't notice this at first glance, but from your code above:

 document.Open(); PdfWriter.GetInstance(document, fs); 

The order must be canceled:

 PdfWriter.GetInstance(document, fs); document.Open(); 

In other words, you need PdfWriter before trying to open Document

+13
source

Source: https://habr.com/ru/post/1344413/


All Articles