Possibility to alter http headers when we generate PDF

Created on 10 March 2021, almost 4 years ago
Updated 21 June 2024, 7 months ago

Problem/Motivation

I need to set http headers for print/pdf/node/* route for some reason, but I cannot do it with Entity Print , because the module uses symphony StreamedResponse without third parameters $headers. Now I can this only one way by extending EntityPrintController class and altering routes with RouteSubscriber, but I think it is not proper way.

Proposed resolution

The implementing hook to set second (response status) and third (http headers) parameters for StreamedResponse is best way to resolve this issue.

✨ Feature request
Status

Postponed: needs info

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡¦Ukraine vitalius2009

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¨πŸ‡¦Canada endless_wander

    I used this functionality successfull to add headers for my use case of wanting X-Robot-Tag added to every PDF generated by the module.

    Here is an example

    added to module services.yml:

    my_custom.events:
        class: Drupal\my_custom\EventSubscriber\MyEventSubscriber
        tags:
          - { name: event_subscriber }
    

    Then in the modules/custom/my_custom/src/EventSubscriber/MyEventSubscriber.php:

    namespace Drupal\my_custom\EventSubscriber;
    
    use Drupal\entity_print\Event\PrintEvents;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Symfony\Component\EventDispatcher\GenericEvent;
    
    /**
     * Add custom headers.
     */
    class MyEventSubscriber implements EventSubscriberInterface {
    
      public static function getSubscribedEvents() {
        $events[PrintEvents::CONFIGURATION_ALTER][] = ['onEntityPrintConfigurationAlter'];
        return $events;
      }
    
      /**
       * React to an event fired when retrieving a Print engine configuration.
       *
       * @param \Symfony\Component\EventDispatcher\GenericEvent $event
       *   Generic event containing the print engine configuration.
       */
      public function onEntityPrintConfigurationAlter(GenericEvent $event) {
        $configuration = $event->getArgument('configuration');
        $configuration['headers']['X-Robot-Tag'] = 'noindex,nofollow,noarchive';
        $event->setArgument('configuration', $configuration);
      }
    }
    

    This was enough for me to have the headers appear on every PDF.

  • πŸ‡ΊπŸ‡¦Ukraine Terry_Kolodiy

    Changes from MR in combination with the event subscriber from #8 ✨ Possibility to alter http headers when we generate PDF Postponed: needs info work great for me.
    Thanks!

  • πŸ‡¨πŸ‡¦Canada endless_wander

    I believe there is a typo in my code from #8 -- should be "X-Robots-Tag" and not "X-Robot-Tag"

Production build 0.71.5 2024