Score:1

Create a PDF from a node after the node is deleted

uy flag
Nyn

I want to create a PDF from a node when the node is deleted.

I am rendering the node using the following code.

$entity = \Drupal::entityTypeManager();
$view_builder = $entity->getViewBuilder('node');
$build = $view_builder->view($node);
$output = \Drupal::service('renderer')->render($build);

The output comes from node.html.twig, whereas the actual template file should be node--content-type--full.html.twig.

I am using the Dompdf\Dompdf class to create and store the PDF file.

$options = new Options();
$options->setIsHtml5ParserEnabled(true);
$options->set('isRemoteEnabled', TRUE);
$dompdf = new Dompdf($options);
$dompdf->load_html($node_html);
$dompdf->set_paper('A4', 'portrait');
$dompdf->render();

How do I get the exact design of the node?

sonfd avatar
in flag
`$build = $view_builder->view($node, 'full');` to render the Full content view mode.
cn flag
When you delete the node, what theme is active? Is is the same one with your overridden template file?
apaderno avatar
us flag
Welcome to Drupal Answers! Which hook are you using to render a node after it has been deleted? As @Clive said, probably Drupal is using the administrative theme for the confirmation page; that's why the template file is not the expected one.
uy flag
Nyn
$build = $view_builder->view($node, 'full'); this doesn't work. It's the same result
uy flag
Nyn
I am using hook_node_delete.
za flag
After you create the pdf (render()), you will need to get the name and save the file to a directory correct? This post might help: https://drupal.stackexchange.com/a/309049/8667
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.