在C#中,使用Chrome将HTML转换为PDF [英] With C# use Chrome to covert HTML to PDF

查看:394
本文介绍了在C#中,使用Chrome将HTML转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我做错了事,或者这是不可能的.我可以在命令提示符下运行,并使用以下代码中的路径创建pdf格式的文件.有关更多信息,我使用命令行时的参数字符串如下所示: chrome --headless --print-to-pdf ="c:\ Users \ pwtph82 \ desktop \ myreport \ myreport.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:\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();

推荐答案

我不知道为什么它不允许我这样做.但是您可以启动一个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)"), @"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(); 

这篇关于在C#中,使用Chrome将HTML转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆