0

this is my scenario: I have an executable file that convert html files to pdf. This exe work only if you start it from its folder. Example: the exe is in C:\HtmlToPdf, so, in the prompt I will do this:

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

So, there is a way to do this in c#? Because I tried this:

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

But it doesn't work.

Sayse
  • 42,633
  • 14
  • 77
  • 146
SamDroid
  • 601
  • 1
  • 10
  • 28

2 Answers2

3

wkhtmltopdf's documentation/wiki states that it will struggle to find a file if you use its full path. You need to append file:/// to the beginning of the file name

Note that on Windows, you can't use absolute path names with drives for HTML files at the moment:

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

will fail. You need to rather use file:/// URLs:

wkhtmltopdf file:///d:/tmp/x.html x.pdf

This answer may help in the appending

Community
  • 1
  • 1
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • It works like with the Aravind answer, but the images aren't shown in the pdf, there is only a white square where the image should be. – SamDroid Aug 07 '13 at 08:58
  • Yeah I had that problem also but can't remember how I resolved it.. I think I set the working directory of the process to the folder where the html page was, the reason it does it is the html has local paths to the images – Sayse Aug 07 '13 at 09:01
  • I solved it! the solution was to pass like argument only the name of the arguments, not the full path! Thank you anyway! – SamDroid Aug 07 '13 at 09:13
  • No worries, like the first line of my answer says "it will struggle to find the file if you use its full path" :) – Sayse Aug 07 '13 at 09:15
0

From where you are calling the EXE .. .whether its Windows Form application or Webforms....

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 ..

Here is my code

To get Current Exe Executable path

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

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

Its Working Correctly .....

Aravind
  • 1,521
  • 2
  • 12
  • 23
  • Can you let me see the piece of code in the html file where the images are refered? Because in my html the image are in this code: and it is in C:\htmlToPdf\TEST and the exe is in C:\htmlToPdf and it, from c#, doesn't work @AravindSrinivas – SamDroid Aug 07 '13 at 08:53
  • Actually what you need ... Do you want to start the exe by passing the arguments using process class or.. do u need to convert Html to Pdf ....I mean whats your real problem... ? – Aravind Aug 07 '13 at 09:02
  • I have simplified my code first try this and run your exe whether your exe it is taking arguments or not ??? – Aravind Aug 07 '13 at 09:03