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):

<?php
require(plugin_dir_path( __FILE__ ).'fpdf/fpdf.php');   
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');

//$filename="/wp-content/plugins/myplugin/rechnungen/rechnung.pdf";
//$pdf->Output($filename,'F');

//testing output
$pdf->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()….

1 Answer
1

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 )

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *