【问题标题】:ASP.NET MVC - Save Generated HTML as PDF to folderASP.NET MVC - 将生成的 HTML 保存为 PDF 到文件夹
【发布时间】:2014-10-17 09:27:28
【问题描述】:

我已经在 MVC ASP.NET C# 中生成了一个 HTML 页面。 (带有 html 助手)

我想自动将此页面保存为特定文件夹中的 pdf

目前,当有人提交表单时,它会发送到数据库,但我也希望 [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);
    }

【问题讨论】:

标签: html asp.net-mvc pdf visual-studio-2013


【解决方案1】:

将其作为对我的其他问题之一的答案发布,但它也适用于此处,因此您可以在这里找到感兴趣的人。

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

【讨论】:

    猜你喜欢
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-01
    • 2010-12-18
    • 1970-01-01
    • 2023-03-29
    • 2013-06-11
    相关资源
    最近更新 更多