带有HTML字符串或文件的Chrome无头printtopdf?
我正在研究基于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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过大量的测试,我看到我可以通过接受Web URL的HTML文件的路径。据我所知,在任何地方都没有记录下来,但是只要路径正确地格式化,它确实可以工作。这仅在Windows(11)上测试,但我相信它也适用于MacOS,只要所有路径都是正确的。
为了使此工作起作用,
- 无头
必须存在标志,否则它将在您的浏览器中打开文件。可以在其他参数中添加其他输出选项。
请参阅此处以获取示例。
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.
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.