1

In my controller, I am rendering a view.

My Action method looks like this:

public ActionResult SomePrint(Model model)
{
   //Some business action
   return View("viewname",model); 
}

Now my requirement is to save this view as file(may be pdf file) in my solution and send it to print and delete the file once the print is done.

Tried to use Rotativa and convert it to pdf by following

public ActionResult DownloadViewPDF()
{
   var model = new GeneratePDFModel();
     //Code to get content
   return new Rotativa.ViewAsPdf("GeneratePDF", model){FileName = "TestViewAsPdf.pdf"}
}

But i need it to save it as pdf and print the same.

Any help? Thanks in advance.

Santosh
  • 2,355
  • 10
  • 41
  • 64
  • You cannot do this. You can either send a file for download to the browser and hope the user prints it, _or_ send an HTML document that shows the print dialog through JavaScript. _On_ that HTML document, you could display a PDF file in an iframe. – CodeCaster Feb 06 '15 at 15:30

2 Answers2

0

If you would have been requesting to export to a known convertible type (such as Excel), formatting the stream would be enough. But if you would like to Export to PDF you should create another View to Export the file and use a 3rd party application such as iText.

user3021830
  • 2,784
  • 2
  • 22
  • 43
0

You can use BuildPdf method on ViewAsPdf.

public ActionResult DownloadViewPDF()
    {
        var model = new GeneratePDFModel();
        var pdfResult = new ViewAsPdf("GeneratePDF", model) 
                          { FileName = "TestViewAsPdf.pdf" };

        var binary = pdfResult.BuildPdf(this.ControllerContext);

        // you can save the binary pdf now

        return File(binary, "application/pdf");
    }
Giorgio Bozio
  • 2,982
  • 3
  • 20
  • 20
  • what is GeneratePDF here? – Santosh Feb 13 '15 at 14:08
  • @Santosh This should be the accepted answer. I am working on something very similar to your scenario. Once the file is returned you can save it or print it or do nothing with it. – Ju66ernaut Feb 25 '15 at 21:00