D8 Programmatically change page orientation for Entity Print module In many projects, some entities needs to be printed on LANDSCAPE format, others, on PORTRAIT.
I was searching for an example or, maybe, a more precise way to implement a page orientation hook, but didn't really find any. 
I just need to do something like 

if($entity_type = "foo"){ set page orientation to portrait }

On this issue, benjy wrote :
If you need to do this programatically, see PrintEvents::CONFIGURATION_ALTER

I looked at the code and saw TestEngineConfigurationAlter class. So I tried to replicate this in a custom module, but it doesn't seem to do anything. I'm obviously missing a piece of the puzzle and I'm humbly asking for help.

Thank you very much :)



Related issues on drupal.org


2797877: Setting paper orientation for dompdf?
2715875: Setting paper size for dompdf in D8
Ok so here's a solution.

As mentionned, you only need to add an event in a custom module inside 

my_module/src/EventSubscriber/EntityPrintSubscriber.php

I simply use a GET parameter from url to set page orientation. 

ie. yoursite.com/print/pdf/entity_type/entity_id?orientation=portrait

namespace Drupal\my_module\EventSubscriber;

use Drupal\entity_print\Event\PrintEvents;
use Drupal\entity_print\Plugin\PrintEngineBase;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

class EntityPrintSubscriber implements EventSubscriberInterface {

  public function alterConfiguration(GenericEvent $event) {
    //ie. /print/pdf/entity_type/entity_id?orientation=portrait
    $request = \Drupal::request()-&gt;query-&gt;get('orientation');
    if(!empty($request) &amp;&amp; ($request == PrintEngineBase::PORTRAIT || $request == PrintEngineBase::LANDSCAPE)){
      $configuration = $event-&gt;getArgument('configuration');
      $configuration['orientation'] = $request;
      $event-&gt;setArgument('configuration', $configuration);
    }
  }

  public static function getSubscribedEvents() {
    return [
      PrintEvents::CONFIGURATION_ALTER =&gt; 'alterConfiguration',
    ];
  }

}


Edit: 

Don't forget to add a service to your module. 
So inside my_module/my_module.services.yml, add this:

my_module.entityprintconfig:
    class: Drupal\my_module\EventSubscriber\EntityPrintSubscriber
    tags:
      - { name: event_subscriber }