使用wkhtmltopdf将Asp.net代码转换为pdf
本文关键字:转换 pdf 代码 net wkhtmltopdf Asp 使用 | 更新日期: 2024-09-23 11:33:43
我正在尝试将单个asp.net页面转换为pdf。根据我的研究,大多数人说可以使用wkhtmltopdf来完成。我试着使用,但遇到了一些问题。希望你们能帮我。
string myDocumentsPath = "C:''Users''Downloads''wkhtmltopdf.exe ";
ProcessStartInfo psi = new ProcessStartInfo(myDocumentsPath, "
http://localhost/ViewResume.aspx");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
Process myProcess = Process.Start(psi);
myProcess.WaitForExit();
myProcess.Close();
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=abc.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile("D:''bb.pdf");
Response.End();
我得到的错误是:-
The requested operation requires elevation
Description: An unhandled exception occurred during the execution of the current web
request.
Exception Details: System.ComponentModel.Win32Exception: The requested operation
requires elevation
堆栈跟踪:-
[Win32Exception (0x80004005): The requested operation requires elevation]
System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) +1959
System.Diagnostics.Process.Start() +145
System.Diagnostics.Process.Start(ProcessStartInfo startInfo) +49
FinalDen_ViewResume.Button1_Click(Object sender, EventArgs e) in
c:'Users'Samba'Desktop'New folder'FinalDen(latest)'FinalDen'ViewResume.aspx.cs:144
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String
eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +5563
希望有人能帮我。TQ
您得到的错误是因为进程不是以管理员身份运行的。您需要添加runas动词
psi.Verb = "runas";
这将要求当前用户进程以管理员身份运行。如果不是(而且很可能不应该,因为它是asp.net),您将需要模拟另一个用户(这超出了这个问题的范围)。
更多信息可以在这个答案中找到。