Jump to content

Can't use PDF module in ready.php Hook


DanielD
 Share

Recommended Posts

Hello.

When i try to use 

$pdf = $modules->get('RockPdf');

in a form hook in /site/ready.php then i get an 500 Server Error.

The hook without this line works, but when i include it then everything stops.

When i insert everything for creating PDF in a template PHP file in /site/templates/ then it works correctly. It generates the PDF without error:

$pdf = $modules->get('RockPdf');
$pdf->write('TEST');
$pdf->save('./generated_pdfs/' . date('yy-m-d_H_i') . '.pdf');

It's generated in /site/assets/RockPdf/generated_pdfs/.

But this same thing in the /site/ready.php file throws an error:

$forms->addHook('FormBuilderProcessor::emailFormReady',
  function($e) {
    $pdf = $modules->get('RockPdf');
    $pdf->write('TEST');
    $pdf->save('./generated_pdfs/' . date('yy-m-d_H_i') . '.pdf');
  }
); 

I get this from the errors.txt log file:

2020-12-14 11:24:21	guest	PAGENAME	Fatal Error: 	Uncaught Error: Call to a member function write() on null in SITEPATH/site/assets/cache/FileCompiler/site/ready.php:36 Stack trace: #0 SITEPATH/wire/core/WireHooks.php(813): ProcessWire\ProcessWire->{closure}(Object(ProcessWire\HookEvent)) #1 SITEPATH/wire/core/Wire.php(442): ProcessWire\WireHooks->runHooks(Object(ProcessWire\FormBuilderProcessor), 'emailFormReady', Array) #2 SITEPATH/site/modules/FormBuilder/FormBuilder/FormBuilderProcessor.php(1237): ProcessWire\Wire->__call('emailFormReady', Array) #3 SITEPATH/wire/core/Wire.php(386): ProcessWire\FormBuilderProcessor->___emailForm(Object(ProcessWire\InputfieldForm), Array) #4 SITEPATH/wire/core/WireHooks.php(723): ProcessWire\Wire->_callMethod('___emailForm', Array) #5 
SITEPATH/wire/core/Wire.ph (Zeile 36 in SITEPATH/site/assets/cache/FileCompiler/site/ready.php)

If someone could help me, I would greatly appreciate it. My goal is to generate a PDF on form submission and attach it to the email that is sent to the admin.
 

Thanks in advance.

Daniel

Link to comment
Share on other sites

Another thing that I found is than when I put the PDF code outside the $forms->addHook(); then it generates the PDF file.

But if i try to put $pdf = $modules->get('RockPdf'); outside and $pdf->write('TEST'); and $pdf->save(); inside the hook then i get the same error..

Link to comment
Share on other sites

Well it looks like that i solved it. 

We need to declare the $pdf variable outside the hooks, at the start of the file, and include it in the hooks.

$pdf = $modules->get('RockPdf');

$forms->addHook('FormBuilderProcessor::emailFormReady',
  function($e) use ($pdf) {
    $pdf->write('TEST');
    $pdf->save('./generated_pdfs/' . date('yy-m-d_H_i') . '.pdf');
  }
); 

Not it works like a charm ?.

Link to comment
Share on other sites

1 hour ago, DanielD said:

But this same thing in the /site/ready.php file throws an error:

In programming, it is called variable scope.

1 hour ago, DanielD said:

$forms->addHook('FormBuilderProcessor::emailFormReady', function($e) { $pdf = $modules->get('RockPdf'); $pdf->write('TEST'); $pdf->save('./generated_pdfs/' . date('yy-m-d_H_i') . '.pdf'); } );

This throws an error because you are using the variable $modules inside the anonymous function and that function does not know what $modules is :-).

This should work:

<?php

$forms->addHook('FormBuilderProcessor::emailFormReady',
  function($e) {
    //$pdf = $modules->get('RockPdf'); <= DOES NOT WORK. function does not know 'modules'
    $pdf = wire('modules')->get('RockPdf');// <= WORKS. function knows GLOBAL FUNCTION wire()
    $pdf->write('TEST');
    $pdf->save('./generated_pdfs/' . date('yy-m-d_H_i') . '.pdf');
  }
); 

PHP variable scope

https://www.php.net/manual/en/language.variables.scope.php

wire() Function

https://processwire.com/api/ref/functions/wire/

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...