2

I have a requirement to generate a pdf document on a button click on using visual web part- sharepoint 2010. I am using the the open source library http://www.itextpdf.com/ for the same. I am able to execute the below code using the project type as a Windowns Application. But, when I want do it on a button click , I am receiving c:\windows\system32\inetsrv\Test.pdf' is denied. error.

Below is the code that I am using

 public static void Main(string[] args)  --Console Application
    {
        Console.WriteLine("PDF demo");
        Document myDoc = new Document(PageSize.A4.Rotate());
        try
        {
            PdfWriter.GetInstance(myDoc, new FileStream("Salman.pdf", FileMode.Create));
            myDoc.Open();
            myDoc.Add(new Paragraph("First pdf File made by Salman using Itext"));
        }
        catch (DocumentException ex)
        {
            Console.Error.WriteLine(ex.Message);
        }
        myDoc.Close();
    }
}

But I want do the same on a button click event.

    protected void pdfGenerator_OnClick(object sender, EventArgs e)
    {
        Document myDoc = new Document(PageSize.A4.Rotate());
        try
        {
            PdfWriter.GetInstance(myDoc, new FileStream("Salman.pdf", FileMode.Create));---I get an error here
            myDoc.Open();
            myDoc.Add(new Paragraph("First pdf File made by Salman using Itext"));
        }
        catch (DocumentException ex)
        {
            Console.Error.WriteLine(ex.Message);
        }
        myDoc.Close();
    }
}

Please help me , i dont understand the reason for the error. I am tyring it for the first time.

Janet
  • 1,391
  • 14
  • 40
  • 62

2 Answers2

3

The problem is you are trying to create a PDF in a directory you do not have access to. This is because you are not specifying the directory to save the path; which causes it to default in the same location as the current working directory. In many cases, such as for ASP.NET; the default is the same as the location of the process. For ASP.NET, the process is w3wp.exe and resides in c:\windows\system32\inetsrv. Most identities that w3wp will run as (Network Service or AppPoolIdentity) do not have access to that directory. In fact, only system administrators do.

You need to save it somewhere else that the process has write permissions to here:

PdfWriter.GetInstance(myDoc, new FileStream("Janaki.pdf", FileMode.Create))

Should be something like:

PdfWriter.GetInstance(myDoc, new FileStream(@"C:\directoryIcanWriteTo\Janaki.pdf", FileMode.Create))

If you want to save it in the same place as the website, you would use HttpContext.Current.Server.MapPath:

    PdfWriter.GetInstance(myDoc, new FileStream(HttpContext.Current.Server.MapPath("~/Janaki.pdf", FileMode.Create))

Regardless, the identity will still need write permissions to the directory if it doesn't have them. But you definitely shouldn't put them in inetsrv.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Or `Path.Combine(DIRECTORYICANWRITETO, @"Janaki.pdf")` , if `DIRECTORYICANWRITETO` is a variable. – Brian Oct 13 '11 at 19:33
  • note that you also could elevate the privileges so that the current user has access to the directory using the RunWithElevatedPrivileges Method http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx – int32 Oct 13 '11 at 19:56
  • @AndreasScharf Yes, that is a possibility - but an ASP.NET application should *really* not be writing to a system directory. For that to work it would mean you'd have to elevate to someone with Administrative permissions, completely violating the [principle of least privilege](http://en.wikipedia.org/wiki/Principle_of_least_privilege) – vcsjones Oct 13 '11 at 20:19
  • Thanks for your reply. Once the user clicks on the button, I would like to open the pdf file directly instead of getting it saved on the users desktop. How do I achieve this ? – Janet Oct 13 '11 at 20:55
0

Just don't use "FileStream" which requires write permissions on the server. Replace it with MemoryStream and then flush its content on the user's browser.

VahidN
  • 18,457
  • 8
  • 73
  • 117