0

I'm trying to have iTextSharp create a PDF file of the web page and save it to the users computer C drive. This works in debug but when I publish it to the server files never get saved on the computer but the application is saying that it did save it. I think I need to set my path differently. This is in MVC4 C#.

var pdfDoc = new Document();

const string folderPath = "C:\\SG-ListingPDFs\\";

bool isExists = Directory.Exists(folderPath);

if (!isExists)
    Directory.CreateDirectory(folderPath);


var writer = PdfWriter.GetInstance(pdfDoc, new FileStream("C:\\SG-ListingPDFs\\" + detailsViewModel.ListingNumber + ".pdf", FileMode.Create));
                pdfDoc.Open();

//Code here

pdfDoc.Add(table);

TempData["SavedPdF"] = true;
pdfDoc.Close();
Prime
  • 2,410
  • 1
  • 20
  • 35
NNassar
  • 485
  • 5
  • 11
  • 25
  • does the web server have permission to write to that path? – Daniel A. White Nov 27 '13 at 14:51
  • I've created a library that you can use to do this, might be of some use to you. http://stackoverflow.com/questions/16517171/convert-html-to-pdf-in-mvc-with-itextsharp-in-mvc-razor/20198939#20198939 Does not save to disc but sends to the client. – hutchonoid Nov 27 '13 at 15:25

1 Answers1

1

No web server technology has the ability to save a file directly the users computer. What you would need to do is save the file to the server (which you are doing now) and then send a download to the users browser.

To do that in asp.net mvc..

public FileResult GetFile()
{
    byte[] filebytes;

    //load file data however you please
    return File(filebytes, "application/pdf");
}  

If you dont want want to save the file to the local disk, you could also generate your pdf in memory and send it straight out.

Create PDF in memory instead of physical file

Community
  • 1
  • 1
iamkrillin
  • 6,798
  • 1
  • 24
  • 51
  • I see, this makes sense. I am new to this so I don't understand your code but I do have questions for you. If the files are being stored on the server first does that mean server will run out of space eventually? I see how to create the PDF in memory but it's the end peace that I don't understand, how do you save the inmemory PDF? Can you show me a more elaborative code? – NNassar Nov 27 '13 at 16:14
  • An in memory file is pushed out to the browser, if the user wishes to save it as a physical file they can choose to via the browser. – hutchonoid Nov 27 '13 at 16:49