asp.net Html Dynamic,c# - Save dynamically generated HTML page ASP.NET - Stack Overflow

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值