1

I've generated an HTML page in MVC ASP.NET C#. (with html helpers)

I would like to automaticly save this page as a pdf in a specific folder

Currently, when someone submits a form it gets send to a DB, but I also want that [HttpPost] to turn that submitted form to a pdf

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

How do i save this as a pdf?

    private string datadir = null;
    private string wkhtmltopdf = null;

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Person person)
    {
        datadir = ConfigurationManager.AppSettings["datadir"];
        wkhtmltopdf = ConfigurationManager.AppSettings["wkhtmltopdf"];

        if (ModelState.IsValid)
        {
            db.People.Add(person);
            db.SaveChanges();


            //here the PDF should be created
            System.IO.File.WriteAllText("details/" + person.ID +".html"));

            var pdf1 = new ProcessStartInfo(wkhtmltopdf);

            pdf1.CreateNoWindow = true;
            pdf1.UseShellExecute = false;
            pdf1.WorkingDirectory = datadir + "tmp\\";
            pdf1.Arguments = "-q -n --disable-smart-shrinking Pdf." + person.ID + ".html Pdf." + person.ID + ".pdf";

            using (var process = Process.Start(pdf1))
            {
                process.WaitForExit(99999);
                Debug.WriteLine(process.ExitCode);
            }

        return View(person);
    }
1

1 Answer 1

1

posted it as an answer to one of my other questons, but it also applies here so here you go 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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.