Problem/Motivation

Hi , i try to use this snippet in my custom module, with PDF Tools enabled and get this error.

// Convert an entity to a PDF file.
$generator = \Drupal::service('pdf_tools.generator');
$uri = $generator->generateFromUrl(
Url::createFromRoute(
'entity.node.canonical',
[
'node' => entity_load('node', 1),
],
[
'absolute' => TRUE,
]
)->toString(),
$options
);

Error

[Sat Aug 19 20:03:11.256582 2023] [php:notice] [pid 18354] [client ::1:58706] Uncaught PHP Exception Error: "Call to undefined method Drupal\\Core\\Url::createFromRoute()" at /var/www/html/drupal---n/drupal---/web/modules/custom/customPdf/customPdf.module line 39, referer: http://localhost/---/---/web/admin/config/development/performance

thanks

Comments

Cangurin limpiezas created an issue. See original summary.

keshavv’s picture

Status: Active » Needs review

@Cangurin limpiezas
URL::createFromRoute method is deprecated, Use Url::fromRoute instead.

I have modified your code. Now it will work.

// Convert an entity to a PDF file.
$generator = \Drupal::service('pdf_tools.generator');
$uri = $generator->generateFromUrl(
  Url::fromRoute(
    'entity.node.canonical',
    [
      'node' => 1,
    ],
    [
      'absolute' => TRUE
    ]
  )->toString(),
  $options
);
keshavv’s picture

@rlmumford, Please update the deprecated code from Module page. AS mentioned above.

rlmumford’s picture

Thanks, that is updated.

rlmumford’s picture

Status: Needs review » Fixed
keshavv’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.