With C# use Chrome to covert HTML to PDF

时光总嘲笑我的痴心妄想 提交于 2019-12-23 05:41:38

问题


I think I'm doing something wrong or this isn't possible. I am able to run from the command prompt and create pdf fine using the paths in the code below. For more info the argument string when I use the command line looks like: chrome --headless --print-to-pdf="c:\Users\pwtph82\desktop\myreport\myreport.pdf" https://google.com

Thanks for any help in advance.

     System.Diagnostics.Process process = new System.Diagnostics.Process();
        process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        process.StartInfo.FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";

        string arguments = @"chrome --headless --print-to-pdf=""c:\\Users\pwtph82\desktop\myreport\myReport.pdf"" https://google.com";
        process.StartInfo.Arguments = "/C " + arguments;
        process.Start();

回答1:


I don't know why it doesn't allow me to do that. But you can start a powershell instance and run it through powershell:

var process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
var chrome = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles(x86)"), @"Google\Chrome\Application\chrome.exe");

// use powershell
process.StartInfo.FileName = "powershell";
// set the Chrome path as local variable in powershell and run
process.StartInfo.Arguments = "$chrome='" + chrome  + @"'; & $chrome --headless --print-to-pdf='c:\Users\" + Environment.UserName + @"\desktop\myReport.pdf' https://google.com";
process.Start(); 


来源:https://stackoverflow.com/questions/53324060/with-c-sharp-use-chrome-to-covert-html-to-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!