使用 C# 使用 Chrome 将 HTML 转换为 PDF [英] With C# use Chrome to covert HTML to PDF
问题描述
我认为我做错了什么或者这是不可能的.我能够从命令提示符运行并使用下面代码中的路径创建 pdf 文件.有关更多信息,我使用命令行时的参数字符串如下所示:chrome --headless --print-to-pdf="c:Userspwtph82desktopmyreportmyreport.pdf" https://google.com
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:Userspwtph82desktopmyreportmyreport.pdf" https://google.com
提前感谢您的帮助.
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = @"C:Program Files (x86)GoogleChromeApplicationchrome.exe";
string arguments = @"chrome --headless --print-to-pdf=""c:\Userspwtph82desktopmyreportmyReport.pdf"" https://google.com";
process.StartInfo.Arguments = "/C " + arguments;
process.Start();
推荐答案
我不知道为什么它不允许我这样做.但是你可以启动一个powershell实例并通过powershell运行它:
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)"), @"GoogleChromeApplicationchrome.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 + @"desktopmyReport.pdf' https://google.com";
process.Start();
这篇关于使用 C# 使用 Chrome 将 HTML 转换为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!