Search code examples
google-chromeubuntuchromiumheadlessgoogle-chrome-headless

Unable to use --screenshot or --print-to-pdf using headless mode of Chromium on Ubuntu desktop


I am using the headless version of Chrome and Chromium to take screenshots of given pages. It works fine on Windows and Ubuntu server, but not on an Ubuntu Desktop VM.

I get the error "Open multiple tabs is only supported when remote debugging is enabled" on Ubuntu Desktop when I try to take a screenshot (--screenshot flag) or to generate a PDF file (--print-to-pdf flag).

I could enable remote debugging with the --remote-debugging-port flag, but when it is enabled, you can not use the --screenshot flag or the --print-to-pdf flag.

Any ideas on how I could use these flags on Ubuntu desktop ?

Details

On windows

Version :
Google Chrome Version 84.0.4147.125

Command used :
> start chrome --headless --screenshot=%CD%\example_sh.png https://example.com/

Note: you have to give a path to the screenshot on windows or else it will not create the file, this is why I added %CD%\ on Windows only. But this is not the issue here.

Command output :
None

File generated :
Yes

On Ubuntu Server

Version :

$ chromium-browser --version
Chromium 84.0.4147.105 Built on Ubuntu , running on Ubuntu 18.04

Command used :
$ chromium-browser --headless --screenshot=example_sh.png https://example.com/

Command output :
[0812/102830.971006:INFO:headless_shell.cc(615)] Written to file example_sh.png.

File generated :
Yes

On Ubuntu Desktop

Version :

$ chromium-browser --version
Chromium 84.0.4147.105 snap

Command used :
$ chromium-browser --headless --screenshot=example_sh.png https://example.com/

Command output :
[0812/123941.367535:ERROR:headless_shell.cc(174)] Open multiple tabs is only supported when remote debugging is enabled.

File generated :
No


Note that a solution consisting of replacing .deb packages is not what I want. This could be a good workaround if I were the only one using these flags, but this won't do as a solution, as I'm calling a command using them inside some code that is part of a library that is shared with other people.


Solution

  • I would suggest:

    1. open chromium interactive with: chromium-browser https://example.com/ or if that fails just chromium-browser.

    2. Go to chrome://version in a tab in this new session and note Command Line and Executable Path.

    3. Look for unusual spaces in the command line, such as from an argument with a file path that is not properly escaped. (This can be added to the question and issue after sanitizing any usernames/etc.)

    4. Using the binary indicated in Executable Path directly try to construct your --headless command. If the cause was from a wrapper this should work, if not, using that binary interactively, look at the chrome://version -> Command Line again. There may not be a good workaround if the mangling comes from things it is compiled to inject in to its own command line.