2014-10-17 100 views
1

我在MVC ASP.NET C#中生成了一個HTML頁面。 (與HTML傭工)ASP.NET MVC-將生成的HTML保存爲PDF到文件夾

我想這個頁面全自動保存爲PDF中的特定文件夾

目前,當有人提出它獲取發送到DB形式,但我也希望這樣[HttpPost]把這一形式提交到PDF

例子:http://example1234.com/Persons/details/15

我如何將此保存爲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); 
    } 
+0

你檢查了http://stackoverflow.com/questions/779430/asp-net-mvc-how-to-get-view-to-generate-pdf? – Spock 2014-10-17 12:07:54

回答

1

張貼作爲一個答案,我的其他questons之一,但它也適用於這裏所以在這裏你去爲那些有興趣。

[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); 
}