简体   繁体   中英

The system cannot find the file specified but works when direct in command line

I am struggling to use Chromes print to PDF feature via headless browsing.

My code is very simple

var command = "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe --headless --disable-gpu --print-to-pdf=\"D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\"   http://localhost/mypage";
Process.Start(command);

When I view my command and copy the string, and paste that into a command prompt, it works fine.

This is all on one system so I don't understand why it works in command prompt and not in my C# web app.

Try this

ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:\Program Files(x86)\Google\Chrome\Application\chrome.exe";
proc.Arguments = @"--headless --disable-gpu --print-to-pdf=\""D:\\GitHub\\MySite\\bin\\Debug\\Temp\\createPdf180303084003.pdf\""   http://localhost/mypage";
Process.Start(proc);

The system is now searching for the file "C:\\\\Program Files(x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe --headless --disable-gpu --print-to-pdf=\\"D:\\\\GitHub\\\\MySite\\\\bin\\\\Debug\\\\Temp\\\\createPdf180303084003.pdf\\" http://localhost/mypage" , but you want it to launch "C:\\\\Program Files(x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe" with some arguments. What you need to do is:

Process process = new Process();
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = "arguments";
process.Start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM