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

All we need is an easy explanation of the problem, so here it is.

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?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 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 ,a user expressed his suggestion to add print-options parameter in comment #50

would it really be such trouble to add switch like:


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 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
and listed in the Commited link, We can go through following lines of code. 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, 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 {
       : landscape(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.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply