0

I am saving HTML content to PDF with the following code:

public void SaveHTMLToPdf(string HTML, string FilePath)
{
    Document document = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

    PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\Invoice_Statement.pdf", FileMode.Create));
    document.Open();
    iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(Server.MapPath(ImgCom.ImageUrl.ToString()));

    pdfImage.ScaleToFit(150, 100);

    pdfImage.Alignment = iTextSharp.text.Image.UNDERLYING; pdfImage.SetAbsolutePosition(40, 770);

    document.AddTitle("Invoice Details");
    document.Add(pdfImage);

    iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
    iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);

    styles.LoadTagStyle("th", "color", "red");
    styles.LoadTagStyle("th", "frontsize", "5");

    document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css"));

    hw.Parse(new StringReader(HTML));

    document.Close();
}

But it gives this error:

The process cannot access the file 'C:\inetpub\wwwroot\abc\Invoice_Statement.pdf' because it is being used by another process.

At this line:

PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + 
                      "\\Invoice_Statement.pdf", FileMode.Create));

It happen only on server with IIS-7 and works fine on local with visual studio.

Can anyone help..??

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105

1 Answers1

0

Likely, it is because you do not have permissions to write the file to that location. Keep in mind that when you run locally using Visual Studio, the IIS is running within your currently logged in security context, and you likely have the required permissions to read/write to the file location. Once you deploy to the server, the server usually runs IIS under the security context of the Network Service account or the Local System account. You want to verify that your directory has granted read/write permissions to these two accounts.

user565494
  • 11
  • 1