I hope someone can help me.

What I would like to achieve:
I would like to create an automatic PDF invoice, as soon as an order has taken place and download it to a folder. It DOES NOT have to be displayed.

My problem:
I'm only concerned about the PDF creation. Somehow I can not create and download the PDF. I already read through similar questions on the subject here, but was not able to figure out the issue.

What I've done so far:
I have a plugin, into which the automatic billing has to be integrated. Within the plugin folder (.../wp-content/plugins/myplugin/) I have created a folder fpdf and installed fpdf.

The fpdf.php is located in ... /myplugin/fpdf/fpdf.php

I have created a file with the following code for testing. This is also in the fpdf folder (... /myplugin/fpdf/pdf_creation.php):

require(plugin_dir_path( __FILE__ ).'fpdf/fpdf.php');   
$pdf=new FPDF();
$pdf->Cell(40,10,'Hello World!');

//$filename = '/wp-content/plugins/myplugin/rechnungen/rechnung.pdf';

//testing output

What I get:
If I try to open the PDF directly in the browser, I get a http ERROR 500 (Chrome), in Firefox a white page.

Fatal error: Uncaught Error: Call to undefined function plugin_dir_path()....

  • If you get error 500, then it is a server side error in your code, and you should check your server PHP error log to see which error is causing the problem. We can't help you without knowing the exact problem. – Milan Petrovic Oct 19 '17 at 11:02
  • Thanks for your reply. I get the "Fatal error: Uncaught Error: Call to undefined function plugin_dir_path()...." on line 6. I am using the function without any problems in other files of this plugin. – Sin Oct 19 '17 at 12:08
  • This function doesn't existe if you are outside off WordPress. To use all these functions, create the file using the AJAX system of WordPress : codex.wordpress.org/AJAX_in_Plugins – mmm Oct 19 '17 at 12:39

You need to load Wordpress before using wp functions, use this just after the <?php tag: require( '<<DIR>>/wp-load.php' ); where <<DIR>> is the path to the main wordpress folder (in your case something like ../../../, add double dots as needed to navigate up the plugins and wp-content hierarchy

NOTE: direct linking outside you control zone (i.e. you plugin directory) is not the best thing to do, as it might break in the future. What can you do is write the code in the plugin file(s), wrap it in a function and hook it to a wordpress action (like an ajax form action, see here )

  • this is unportable way based on guessing where the plugin is located relative to wordpress core files, which might or might not work. – Mark Kaplun Oct 19 '17 at 13:21
  • yes, but if he needs it just for testing then it's fine. – Fabrizio Mele Oct 19 '17 at 15:20
  • this file direct inclusion can stop working in the futur then it's better to not use this. – mmm Oct 19 '17 at 16:43
  • the problem is that you might be aware to it, but other people that see it as an accepted answer will not and than their code breaks and they have no idea why. Direct access should never be done, it is like not using "goto", you just do not do it even for testing not to get into the habit of doing it. – Mark Kaplun Oct 19 '17 at 17:02
  • seems legit, editing answer – Fabrizio Mele Oct 19 '17 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.