How can I print a webpage in landscape mode using Headless-Chromium on the command line in Linux?

6,622

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.

Share:
6,622
Alejandro Diego
Author by

Alejandro Diego

Updated on September 18, 2022

Comments

  • Alejandro Diego
    Alejandro Diego almost 2 years

    I am trying this command. It works well.

    chromium-browser  --headless  --disable-gpu --print-to-pdf=file14.pdf file:///input.html
    

    However, it always prints in portrait mode, but I want to print the web page in landscape mode.

    Is there any parameter or argument for passing mode (portrait or landscape) to the Headless-Chromium on the command line so that we can print any web page accordingly in either portrait or landscape mode?

    • C0deDaedalus
      C0deDaedalus over 6 years
      Well Your Command works exactly how it should and I tried to print current page using command you wrote and It worked perfect except for giving some Warning related to render_frame_host_impl.cc
    • C0deDaedalus
      C0deDaedalus over 6 years
      Please add how the final content after printing looks and how you exactly need.
    • Alejandro Diego
      Alejandro Diego over 6 years
      configure printing in chromium for portrait or landscape
  • Alejandro Diego
    Alejandro Diego over 6 years
    very thankyou this help me ; pd:happy new year
  • C0deDaedalus
    C0deDaedalus over 6 years
    Good to see that anwer helped you. What does pd: means ?
  • Alejandro Diego
    Alejandro Diego over 6 years
    pd is postdate .