带有HTML字符串或文件的Chrome无头printtopdf?

发布于 2025-01-24 10:00:01 字数 719 浏览 3 评论 0原文

我正在研究基于Unity3D的应用程序,需要从HTML代码生成PDF。我尝试了其他图书馆,但它们要么超级慢,过时或疯狂地昂贵。 因此,我正在考虑使用Chrome的PrintTopDF功能,该功能正好构建。

对于测试,以下代码可以生成Google HomePage(或任何网站)的PDF,但是我需要将HTML字符串转换为PDF。

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

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

是否可以使用此方法输入带有HTML代码的字符串或本地存储的独立HTML文件转换为PDF?

谢谢!

I'm working on a Unity3D based application and need to generate PDFs from HTML code. I tried other libraries but they're either super slow, outdated, or crazy expensive for my purposes.
Because of that, I'm considering using Chrome's PrintToPDF feature which is built right in.

For testing, the following code works to generate a PDF of Google's homepage (or any site), but I need to convert an HTML string to a PDF.

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

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

Is it possible to input either a string with the HTML code or a locally-stored standalone HTML file to convert to PDF using this method?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2025-01-31 10:00:01

经过大量的测试,我看到我可以通过接受Web URL的HTML文件的路径。据我所知,在任何地方都没有记录下来,但是只要路径正确地格式化,它确实可以工作。这仅在Windows(11)上测试,但我相信它也适用于MacOS,只要所有路径都是正确的。

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

string arguments = @"--headless --disable-gpu --print-to-pdf=""C:\path\to\outputPDFFile.pdf"" ""C:\path\to\sourceHTMLFile.html""";
process.StartInfo.Arguments = arguments;
process.Start();

为了使此工作起作用,- 无头必须存在标志,否则它将在您的浏览器中打开文件。

可以在其他参数中添加其他输出选项。
请参阅此处以获取示例。

After much testing, I see that I can pass in the path to an HTML file where it accepts a web URL. As far as I can find this isn't documented anywhere, but it does work as long as the path is formatted correctly. This is only tested on Windows (11), but I believe it works on MacOS as well, as long as all paths are correct.

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

string arguments = @"--headless --disable-gpu --print-to-pdf=""C:\path\to\outputPDFFile.pdf"" ""C:\path\to\sourceHTMLFile.html""";
process.StartInfo.Arguments = arguments;
process.Start();

In order for this to work, the --headless flag needs to be present otherwise it will just open the file in your browser.

Other output options can be added with other arguments.
See here for examples.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文