Unexpected error with print engine PhpWkhtmlToPdf or DomPdf: Session has not been set

Created on 24 August 2023, about 1 year ago
Updated 9 April 2024, 7 months ago

Problem/Motivation

During config import, I'm getting following error,

In ConfigImportCommands.php line 324:

    The import failed due to the following reasons:
    Unexpected error during import with operation update for entity_print.print_engine.phpwkhtmltopdf: Session has not been set.

Or when using the print builder service in a cron job called via drush

Session has not been set.
Trace :
#0 /var/www/xxx/web/modules/contrib/entity_print/src/Plugin/EntityPrint/PrintEngine/DomPdf.php(285): Symfony\Component\HttpFoundation\Request->getSession() 
#1 /var/www/xxx/web/modules/contrib/entity_print/src/Plugin/EntityPrint/PrintEngine/DomPdf.php(238): Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf->setupHttpContext() 
#2 /var/www/xxx/web/modules/contrib/entity_print/src/Plugin/EntityPrint/PrintEngine/DomPdf.php(228): Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf->doRender() 
#3 /var/www/xxxx/web/modules/contrib/entity_print/src/PrintBuilder.php(100): Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf->getBlob() 
#4 /var/www/xxx/web/modules/custom/xxx_commerce/src/SalesJournalGenerator.php(372): Drupal\entity_print\PrintBuilder->savePrintable() 
#5 /var/www/xxx/web/modules/custom/xxx_commerce/xxx_commerce.module(793): Drupal\xxx_commerce\SalesJournalGenerator->generateSalesJournal() 
#6 /var/www/xxx/web/core/lib/Drupal/Core/Cron.php(335): xxx_commerce_cron()

Proposed solution

Check that the request has a session before getting the session.

πŸ› Bug report
Status

RTBC

Version

2.0

Component

Code

Created by

πŸ‡ͺπŸ‡ΈSpain facine

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

Merge Requests

Comments & Activities

  • Issue created by @facine
  • Status changed to Needs review about 1 year ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update about 1 year ago
    54 pass
  • πŸ‡©πŸ‡ͺGermany Tomefa Dresden

    On Drupal 10.1 and entity_print 2.13 i have the same error during configuration import.

    This patch solve the issue and i can now import my configuration without problem.

  • πŸ‡¦πŸ‡ΉAustria nimoatwoodway Tyrol / Austria

    Same here. Drupal 10.1 and entity_print 2.13. Patch #2 fixed it for me! Thenks!

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 11 months ago
    54 pass
  • πŸ‡«πŸ‡·France flocondetoile Lyon

    Hello,

    Same error here with Drupal 10.1.6 and Entity Print 2.13, but with the print engine DomPDF. And during a cron job launched via drush to generate PDFs (which don't have a session).

    /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper */
    $stream_wrapper = \Drupal::service('stream_wrapper_manager');
    $print_engine = \Drupal::service('plugin.manager.entity_print.print_engine')->createSelectedInstance('pdf');
    $print_builder = \Drupal::service('entity_print.print_builder');
    $billfile_uri = $print_builder->savePrintable($orders, $print_engine, 'private', $stream_wrapper->getTarget($file_destination . $bill_filename));

    The stack trace

    Trace :
    #0 /var/www/xxx/web/modules/contrib/entity_print/src/Plugin/EntityPrint/PrintEngine/DomPdf.php(285): Symfony\Component\HttpFoundation\Request->getSession() 
    #1 /var/www/xxx/web/modules/contrib/entity_print/src/Plugin/EntityPrint/PrintEngine/DomPdf.php(238): Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf->setupHttpContext() 
    #2 /var/www/xxx/web/modules/contrib/entity_print/src/Plugin/EntityPrint/PrintEngine/DomPdf.php(228): Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf->doRender() 
    #3 /var/www/xxxx/web/modules/contrib/entity_print/src/PrintBuilder.php(100): Drupal\entity_print\Plugin\EntityPrint\PrintEngine\DomPdf->getBlob() 
    #4 /var/www/xxx/web/modules/custom/xxx_commerce/src/SalesJournalGenerator.php(372): Drupal\entity_print\PrintBuilder->savePrintable() 
    #5 /var/www/xxx/web/modules/custom/xxx_commerce/xxx_commerce.module(793): Drupal\xxx_commerce\SalesJournalGenerator->generateSalesJournal() 
    #6 /var/www/xxx/web/core/lib/Drupal/Core/Cron.php(335): xxx_commerce_cron() 
    

    attached a patch with same fix for DomPdf print engine

  • πŸ‡«πŸ‡·France flocondetoile Lyon

    retitling

  • πŸ‡ΏπŸ‡¦South Africa PatrickMichael

    #2 worked for me, thank you! Drupal 10.1.7, entity_print 8.x-2.13

  • πŸ‡«πŸ‡·France dark05

    Thank you for patch #5 it works!

  • πŸ‡ͺπŸ‡ΈSpain aleix

    #5 worked, It solves the related commerce ticketing send of mail issues

  • Status changed to RTBC 10 months ago
  • πŸ‡©πŸ‡ͺGermany Tomefa Dresden

    #2 solved the issue only for PhpWkhtmlToPdf
    where #5 solved it for both PhpWkhtmlToPdf and DomPdf

    #5 should be used in preference of #2

    With so many people using the patch, i will set the ticket to reviewed.

  • πŸ‡©πŸ‡ͺGermany Tomefa Dresden

    Tomefa β†’ changed the visibility of the branch 3383187-unexpected-error-with to hidden.

  • πŸ‡©πŸ‡ͺGermany Tomefa Dresden

    Tomefa β†’ changed the visibility of the branch 3383187-unexpected-error-with to active.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.0 & MySQL 5.7
    last update 10 months ago
    54 pass
  • Status changed to Needs review 10 months ago
  • πŸ‡©πŸ‡ͺGermany Tomefa Dresden
  • Status changed to RTBC 10 months ago
  • πŸ‡ͺπŸ‡ΈSpain grota

    we are using #5, applies cleanly to 2.12.0 and is IMHO a simple enough patch, moving to RTBC

  • πŸ‡ͺπŸ‡ΈSpain SadySierralta

    PDF was missing from commerce invoice confirmation mail attachments, after applying #5 the error, "Symfony\Component\HttpFoundation\Exception\SessionNotFoundException: Session has not been set.", is no longer appearing and PDF is attached in mail.

    By the way this was happening while cron context.

Production build 0.71.5 2024