0

With ASP.NET MVC, I generated an html page

Example: http://example1234.com/Persons/details/15

Changing the last digit changes the value of the fields which I imported with @HTML helpers

I would like to automatically save this page somewhere to the server, to make it static.

Something like PersonNr15.html with the generated content hardcoded into that page.

      @model MvcApplication3.Models.Person

      @{
      ViewBag.Title = "Details";
      }

      <h2>Details</h2>

     <fieldset>
     <legend>Person</legend>
        <p>@Html.DisplayFor(model => model.FirstName)</p>
        <p>@Html.DisplayFor(model => model.LastName)</p>

      </fieldset>
SNT
  • 293
  • 1
  • 2
  • 13
  • Show some code. Especially the part where you return the HTML to the browser. What did you try? – Alexander Oct 17 '14 at 11:28
  • `I would like to automatically save this page somewhere to the server, to make it static` Just want to know why you would like to save page as static? – Raghuveer Oct 17 '14 at 11:43
  • @irvgk Sorry if my use of English is incorrect on the word "static", But i want to change it to PDF afterwards so the outputted text should just be in the saved html document.. so that if I open Person15.html it would show up the file with the data of person 15. Is it explained better like this? – SNT Oct 17 '14 at 11:55
  • 1
    @user3712713 If you want to render to a PDF then you may not need to render to an intermediate file at all. ABCPDF allows you to pass a URL which it will then render into your file, perhaps worth looking into? – Anduril Oct 17 '14 at 12:15
  • The PDF needs to be automaticly emailed once the person presses submit Submit -> Create dynamic page -> put it into seperate HTML -> change html to pdf with C# and add it as an email attachment I wanted to do dynamic page straight into a pdf as attachment but it doesnt work that way – SNT Oct 17 '14 at 12:25
  • @user3712713 It is certainly possible to go from a dynamically rendered page to a PDF which can then be attached and emailed without ever going through the steps of saving an intermediate HTML file (or even the end PDF if you don't want to save it. What library are you using to generate the PDF? – Anduril Oct 17 '14 at 13:44
  • I'm not using any library yet, but i was looking at iTextSharp.. but i'm totally new to this subject and i was looking for some pointers – SNT Oct 17 '14 at 13:53

2 Answers2

0

What you need to do is render the view to a string, and then save the string to a file like you would any other string. The rendering of an MVC view to a string is covered in previously answered questions on here such as This question

Community
  • 1
  • 1
Anduril
  • 1,236
  • 1
  • 9
  • 33
0

I changed my code myself. I used my self-made template, and changed the words like #NAME# After changing the variable words from the file, save the file, make a PDF out of it. and done. (PDF wasn't part of the question, but i added it in for those interested).

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Person person)

        datadir = ConfigurationManager.AppSettings["datadir"];
        //datadirectory defined in Web.config
        //also possible to hardcode it here, example: "c:/windows/PDFfolder"

        wkhtmltopdf = ConfigurationManager.AppSettings["wkhtmltopdf"];
        //directory to the file "wkhtmltopdf", downloaded it somewhere
        //just like above, defined at web.config possible to hardcode it in

        ViewData["IsModelValid"] = ModelState.IsValid ? "true" : "false";
        //valid checker


        if (ModelState.IsValid)      //check if valid
        {                
        db.People.Add(person);       //add to db

            db.SaveChanges();
        var fileContents1 = System.IO.File.ReadAllText(datadir + "Template.html"); 
        //get template from datadirectory
        fileContents1 = fileContents1.Replace("#NAME#", person.Name);
        //replace '#NAME#' by the name from the database table person.Name

       System.IO.File.WriteAllText(datadir + "tmp\\Template." + person.ID + ".html", fileContents1);
       //create a new html page with the replaced text
       //name of the file equals the ID of the person


            var pdf1 = new ProcessStartInfo(wkhtmltopdf); //start process wkhtmltopdf
            pdf1.CreateNoWindow = true;  //don't create a window
            pdf1.UseShellExecute = false; //don't use a shell
            pdf1.WorkingDirectory = datadir + "tmp\\"; //where to create the pdf
            pdf1.Arguments = "-q -n --disable-smart-shrinking Overeenkomst." + person.ID + ".html Overeenkomst." + person.ID + ".pdf";
          //get the html to convert and make a pdf with the same name in the same directory

        }

        return View(person);
    }
SNT
  • 293
  • 1
  • 2
  • 13