I´ve started using HTML2PDF (https://www.html2pdf.fr/) to create invoices, now the following code works:
try {
ob_end_clean();
ob_start();
include('../faktura/fa.php');
$html = ob_get_contents();
ob_end_clean();
$content = $html;
$html2pdf = new Html2Pdf('P', 'A4', 'cs',true,"UTF-8");
$html2pdf->setDefaultFont('freeserif');
$html2pdf->pdf->SetFont('freeserif');
$html2pdf->writeHTML($content);
$pdfContent = $html2pdf->output('my_doc.pdf', 'S');
} catch (Html2PdfException $e) {
$html2pdf->clean();
$formatter = new ExceptionFormatter($e);
echo $formatter->getHtmlMessage();
}
The content of fa.php
is converted to pdf, however if I change one of the lines to:
include('../faktura/fa.php?id=105');
note that I only added ?id=105
, it returns only a blank page.
The php file fa.php includes:
$id = (int) $_GET['id'];
What I need to do is to pass an ID to that php script so the invoice of the exact order is generated.
Include
basically means "copy the code from that file to here".
So if you change the included file code from $id = (int) $_GET['id']; to $id = $thatId;
And prior to the include() function you'd write $thatId = 105; The included file will be able to access the variable.
File A:
$id = 105;
include('../faktura/fa.php');
File fa.php
//$id = (int) $_GET['id']; // no need this anymore as we've declared a $id variable with a value;
// .. Do something with $id
// .. for example:
echo $id; //will print 105
Or, in case you want fa.php to continue working with QUERY PARAMETER and to work with your invoice (2pdf) code:
if(!isset($id) && isset($_GET['id'])) {
$id = (int) $_GET['id'];
}