我正在努力通过无头浏览使用Chrome打印到PDF功能。
我的代码非常简单
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);
当我查看我的command
并复制字符串,并将其粘贴到命令提示符时,它工作正常。
这是在一个系统上,所以我不明白为什么它在命令提示符下工作,而不是在我的C#Web应用程序中。
试试这个
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);
系统现在正在搜索文件"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"
,但是你希望它用一些参数启动"C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe"
。你需要做的是:
Process process = new Process();
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = "arguments";
process.Start();