简体   繁体   English

以编程方式打印网页

[英]programmatically print a web page

I am making a program to automatically go to a website and print a page, Can't seem to make it work, I tried selenium chrome driver, problem is it doesn't work.我正在制作一个程序来自动转到网站并打印页面,似乎无法使其工作,我尝试了 selenium chrome 驱动程序,问题是它不起作用。 i tried action.sendkeys keys ctrl + shift + p to no avail, the biggest problem is print preview pop-up.我试过 action.sendkeys 键ctrl + shift + p无济于事,最大的问题是打印预览弹出。 I tried sending JavaScript command: window.print() , but the print preview in chrome stands in my way, because you need to press enter.我尝试发送 JavaScript 命令: window.print() ,但 chrome 中的打印预览妨碍了我,因为您需要按 Enter 键。 Is there a way in JavaScript to simulate the pressing of the ENTER key? JavaScript 中有没有办法模拟按下ENTER键? Help would be appreciated.帮助将不胜感激。

Well, after a bit of research, I found this video , if you can add these switches: "--kiosk --kiosk-printing", to the chrome driver start, it would automatically skip the print preview prompt, just as shown in the video.好吧,经过一番研究,我找到了这个视频,如果你可以添加这些开关:“--kiosk --kiosk-printing”,到chrome驱动程序启动,它会自动跳过打印预览提示,如图所示该视频。 also, I tested this on the latest version of SRWare iron(fork of chromium), and it worked.此外,我在最新版本的 SRWare 铁(铬叉)上对此进行了测试,并且有效。

If you are using C# to make your program, then there is an easier solution:如果您使用 C# 编写程序,则有一个更简单的解决方案:

    private void Button1_Click(object sender, EventArgs e)
    {
        PrintHelpPage();
    }

    private void PrintHelpPage()
    {
        // Create a WebBrowser instance. 
        WebBrowser webBrowserForPrinting = new WebBrowser();

        // Add an event handler that prints the document after it loads.
        webBrowserForPrinting.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(PrintDocument);

        // Set the Url property to load the document.
        webBrowserForPrinting.Url = new Uri(@"http://www.google.com"); //This is what you want to change
    }

    private void PrintDocument(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
        // Print the document now that it is fully loaded.
        ((WebBrowser)sender).Print();

        // Dispose the WebBrowser now that the task is complete. 
        ((WebBrowser)sender).Dispose();
    }

Found the answer in Here , this uses the WebBrowser control to navigate to a specific Url, can be a local url or from the internet, and prints it using your default printer.Here 中找到答案,这使用 WebBrowser 控件导航到特定的 Url,可以是本地 url 或来自 Internet,并使用您的默认打印机打印它。

Maybe you could you use this approach that doen't require windows forms, worked like a charm for me: With C# use Chrome to covert HTML to PDF也许你可以使用这种不需要 windows 窗体的方法,对我来说就像一个魅力: 使用 C# 使用 Chrome 将 HTML 转换为 PDF

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(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM