2013-08-07 29 views
0

这是我的场景:我有一个将html文件转换为pdf的可执行文件。 只有当你从它的文件夹中启动它时,这个exe才能工作。 例如:exe文件是在C:\ HtmlToPdf,所以,在提示我会做到这一点:从其文件夹运行exe文件

C:\> cd HtmlToPdf 
C:\HtmlToPdf> htmltopdf.exe htmlFile pdfFile 

所以,有一种方法在C#这样做吗?因为我试过这个:

FileInfo htmlInfo = new FileInfo(executablePath + @"\" + filename); 
var procInfo = new ProcessStartInfo("wkhtmltopdf.exe",htmlInfo.FullName + " " + htmlInfo.FullName.Replace(".html",".pdf")); 

procInfo.WorkingDirectory=executablePath; 
procInfo.UseShellExecute = false; 
Process.Start(procInfo); 

但它不起作用。

+0

我加了标记,以便您的问题更多的Google友好 – Sayse

+0

谢谢! @Sayse – SamDroid

回答

3

wkhtmltopdf's documentation/wiki指出如果使用完整路径,它将很难找到文件。您需要添加file:///到文件名

需要注意的是在Windows上,你不能在此刻使用与HTML文件的驱动器的绝对路径名的开头:

wkhtmltopdf d:\ TEMP \ x.html x.pdf

将会失败。您需要宁愿用文件:///网址:

wkhtmltopdf文件:/// d:/tmp/x.html x.pdf

This answer可能有助于附加

+0

它的工作方式与Aravind的答案类似,但图像不显示在pdf中,图像应该只有一个白色方块。 – SamDroid

+0

是的,我也有这个问题,但不记得我是如何解决它..我认为我设置进程的工作目录到HTML页面所在的文件夹,它这样做的原因是HTML有本地路径图片 – Sayse

+0

我解决了!解决方法是只传递参数的名称,而不是完整的路径!不管怎样,谢谢你! – SamDroid

0

从在您所呼叫的EXE ..。不管它的Windows窗体应用程序或Web表单....

If its windows forms it will work 

else 

if its Webforms like asp.net you have to change the properties of IIS Server to start the exe 

Because due to some security reasons microsoft will not allow you to start the process class from IIS server... but the same code will work from Visualstudio .. 

这里是我的代码

要得到

string sCurrentPAth = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 

    Process.Start(@"E:\Debug\MyExe.exe", "arg1 arg2 arg3"); 

其正常工作电流EXE执行路径.....

+0

接受答案.... !!!!!! – Aravind

+0

你能让我看看HTML文件中引用图像的那段代码吗?因为在我的HTML图像是在这个代码:它在C:\ htmlToPdf \测试和EXE是在C:\ htmlToPdf,它从C#,不工作@AravindSrinivas – SamDroid

+0

其实你需要.. 。你想通过使用过程类或..传递参数来启动exe。你是否需要将Html转换为Pdf ....我的意思是什么是你真正的问题...? – Aravind