1

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
0

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()->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',
    ];
  }

}

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 }
  • Do you just add the GET param manually when you want to print? – steve Dec 8 '17 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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