ASP.NET MVC - Save Generated HTML as PDF to folder -
i've generated html page in mvc asp.net c#. (with html helpers)
i automaticly save page pdf in specific folder
currently, when submits form gets send db, want [httppost] turn submitted form pdf
example: http://example1234.com/persons/details/15
how save 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 pdf should 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); } homecoming view(person); }
posted reply 1 of other questons, applies here here go interested.
[httppost] [validateantiforgerytoken] public actionresult create(person person) datadir = configurationmanager.appsettings["datadir"]; //datadirectory defined in web.config //also possible hardcode here, example: "c:/windows/pdffolder" wkhtmltopdf = configurationmanager.appsettings["wkhtmltopdf"]; //directory file "wkhtmltopdf", downloaded somewhere //just above, defined @ web.config possible hardcode in viewdata["ismodelvalid"] = modelstate.isvalid ? "true" : "false"; //valid checker if (modelstate.isvalid) //check if valid { db.people.add(person); //add db db.savechanges(); var filecontents1 = system.io.file.readalltext(datadir + "template.html"); //get template datadirectory filecontents1 = filecontents1.replace("#name#", person.name); //replace '#name#' name database table person.name system.io.file.writealltext(datadir + "tmp\\template." + person.id + ".html", filecontents1); //create new html page replaced text //name of file equals id of person var pdf1 = new processstartinfo(wkhtmltopdf); //start process wkhtmltopdf pdf1.createnowindow = true; //don't create window pdf1.useshellexecute = false; //don't utilize shell pdf1.workingdirectory = datadir + "tmp\\"; //where create pdf pdf1.arguments = "-q -n --disable-smart-shrinking overeenkomst." + person.id + ".html overeenkomst." + person.id + ".pdf"; //get html convert , create pdf same name in same directory } homecoming view(person); }
html asp.net-mvc pdf visual-studio-2013
No comments:
Post a Comment