使用 C# 使用 Chrome 将 HTML 转换为 PDF
With C# use Chrome to covert HTML to PDF
我认为我做错了什么或者这是不可能的。我可以从命令提示符 运行 并使用下面代码中的路径创建 pdf。有关详细信息,我使用命令行时的参数字符串如下所示:
chrome --headless --print-to-pdf="c:\Users\pwtph82\desktop\myreport\myreport.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)\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();
我不知道为什么它不允许我这样做。但是您可以通过 powershell 启动一个 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();
我认为我做错了什么或者这是不可能的。我可以从命令提示符 运行 并使用下面代码中的路径创建 pdf。有关详细信息,我使用命令行时的参数字符串如下所示: chrome --headless --print-to-pdf="c:\Users\pwtph82\desktop\myreport\myreport.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)\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();
我不知道为什么它不允许我这样做。但是您可以通过 powershell 启动一个 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();