0

I'm trying to convert single asp.net page to pdf. Based on my research, most of people said that it can be done using wkhtmltopdf. I tried to used but facing some problem. Hope you guys can help me with this.

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 error that I'm getting is :-

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

Stack Trace:-

[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

Hope anyone can help me with this. TQ

NOOR MOHAMED
  • 55
  • 1
  • 2
  • 14

1 Answers1

0

The error that you are getting is because the process is not running as an administrator. You would need to add the runas verb

psi.Verb = "runas";

This would require the current user process is running as to be an administrator. If it isn't (and likely shouldn't since it's asp.net), you will need to impersonate another user (which is beyond scope of this question).

More info can be found in this answer.

Community
  • 1
  • 1
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
  • Hi, I tried to add the mentioned code, but it's still showing same error. – NOOR MOHAMED Dec 12 '13 at 17:10
  • This shouldn't need admin at all. – SLaks Dec 12 '13 at 17:11
  • @NOORMOHAMED What user is application pool running as? If that user doesn't have administrator permissions, then you will still get the error, like I said. You can make that account an administrator to test, but I would not suggest running a production asp.net web site under an account that has administrator permissions – Darren Kopp Dec 12 '13 at 17:15
  • @SLaks perhaps, I don't know what wkhtmlpdf requires, just going off of error message – Darren Kopp Dec 12 '13 at 17:15
  • Side note: _Never_ run a public-facing web server as admin. – SLaks Dec 12 '13 at 17:27
  • @DarrenKopp, basically this university project.Since the budget is limited, planning to use localhost during presentation by using my own personal computer. – NOOR MOHAMED Dec 12 '13 at 17:42