How can I print a webpage in landscape mode using Headless-Chromium on the command line in Linux?
Solution 1:
Well, Certainly the Question needs more Research and I am writing down what I've found regarding it. So, It may not be an exact answer and somehow more can be added to it.
How it Started
In the Issue 603559 entitled Headless Chrome: save page to pdf
on bugs.chromium.org ,a user expressed his suggestion to add print-options
parameter in comment #50
would it really be such trouble to add switch like:
-print-options=landscape,displayHeaderFooter,printBackground,scale,paperWidth,paperHeight,
marginTop,marginBottom,marginLeft,marginRight,pageRanges
It's really not just "small wrapper script (in any language with a WebSockets library)" because you have to handle a lot of events during process instead of just invoking a command and wait for it's exit status.
Just below that following Comment #51 by Project Contributer eseckler, clearly states that
Please consider the more general problem - We don't intend to provide all the flexibility that DevTools provides through command line options: There are various technical reasons why command line options cannot provide the same flexibility (e.g. the event-based nature of DevTools). Adding more options won't solve this general misfit.
I think that we should be aiming for a more library-based approach for headless user environments (e.g. nodejs, python, java, ..). Such a library can implement a default, but more customizable, page load/life cycle that includes common functions such as rendering a pdf/screenshot. I think we could provide an example/reference library and application (say, for nodejs) as part of headless chromium. Filed https://crbug.com/719921 to track this discussion.
It is likely that this will lead to deprecation (and eventual removal) of --headless mode command line flags such as --print-to-pdf or --screenshot.
Now You can see Ist line clearly states that
We don't intend to provide all the flexibility that DevTools provide through command-line options
That's very true as Chrome DevTools method Page.printToPDF describes all the parameters that can be used and the first parameter that it states is Landscape (Orientation) that is by-default false.
What's being done by Developers
Now Issue 2829973002 entitled add customized printing setting for headless (Closed)
decsribes 3 things to be added to printing settings for headless in the code. These are
1) Add parameters to printToPDF command, which let the user to specify printing settings like paper size, margin size, etc.
2) PrintWebViewHelper::PrintPageInternal and PrintWebViewHelper::RenderPage feed print_preview_context_.total_page_count() to PrintHeaderAndFooter. However, HeadlessPrintManager issues PrintMsg_PrintPages IPC message, which leaves print_preview_context_ uninitialized. To solve the problem, add page_count as an arg to these two methods.
3) Added unit test and browser test for print to pdf.
Ist point here, states about printing settings like paper size, margin size , etc. but not explicitly states about Orientation Settings.
On the same page, you can see each and every patch set (Total 19) of what has been changed in the codebase. Also, Link Commited shows the final commit of proposed changes to print-settings in codebase, After which
Issue 2829973002 was marked as Closed
. The final commit also lists the files modified for the proposed changes.
So, Is it possible to print in Landscape mode ?
With Reference to source code files page_handler.cc and headless_print_manager.cc listed in the Commited link, We can go through following lines of code.
page_handler.cc Lines 302-316
void PageHandler::PrintToPDF(Maybe<bool> landscape,
Maybe<bool> display_header_footer,
Maybe<bool> print_background,
Maybe<double> scale,
Maybe<double> paper_width,
Maybe<double> paper_height,
Maybe<double> margin_top,
Maybe<double> margin_bottom,
Maybe<double> margin_left,
Maybe<double> margin_right,
Maybe<String> page_ranges,
std::unique_ptr<PrintToPDFCallback> callback) { callback->sendFailure(Response::Error("PrintToPDF is not implemented"));return; }
Here, function PrintToPDF
takes parameter Landscape
whose datatype is bool
meaning either true or false.
Now, headless_print_manager.cc defines all the headless printing functions and it includes headless_print_manager.h in the header which defines the namespace printing
. It defines all the headless printing settings in the fucntion HeadlessPrintSettings
in the structure.
Lines 22-41
struct HeadlessPrintSettings { HeadlessPrintSettings() : landscape(false), display_header_footer(false), should_print_backgrounds(false), scale(1) {} gfx::Size paper_size_in_points; PageMargins margins_in_points; bool landscape; bool display_header_footer; bool should_print_backgrounds; // scale = 1 means 100%. double scale; std::string page_ranges; };
The Answer
You can clearly see in the code above the third line that says
HeadlessPrintSettings(): landscape(false)
So, Printing any Webpage using Headerless-chrome will always print in portrait because by-default landscape mode is set to false. There are many other printing settings also that you can explore through this code.
Note : I know that answer could be a straight NO, but I thought sharing my findings and the relevant references would be better option for everyone.