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

2 Answers 2


Ok so here's a solution.

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


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()->query->get('orientation');
    if(!empty($request) && ($request == PrintEngineBase::PORTRAIT || $request == PrintEngineBase::LANDSCAPE)){
      $configuration = $event->getArgument('configuration');
      $configuration['orientation'] = $request;
      $event->setArgument('configuration', $configuration);

  public static function getSubscribedEvents() {
    return [
      PrintEvents::CONFIGURATION_ALTER => 'alterConfiguration',



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

    class: Drupal\my_module\EventSubscriber\EntityPrintSubscriber
      - { name: event_subscriber }
  • Do you just add the GET param manually when you want to print?
    – Steve
    Dec 8, 2017 at 23:23

There's a bug in Entity Print 2.2 so if the above solution by @JFK doesn't work please see https://www.drupal.org/project/entity_print/issues/3041514

  • I used to work in 2017, but not anymore obviously. Thanks for pointing that out.
    – JFC
    Aug 2, 2021 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.