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;
}
A pdf file does get created, so I know it runs past
using (FileStream fs = new FileStream(tempPDFFile, FileMode.Create))
at the very least.
This code runs just fine in dev and staging, but it throws an error in production. Any thoughts as to why that could be?