HTML2PDF How to pass id to the html site to be converted

38
January 13, 2021, at 04:10 AM

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.

Answer 1

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'];
}
READ ALSO
Prevent Dropdown menu from closing if I click a menu item

Prevent Dropdown menu from closing if I click a menu item

I have a drop down menu in sidebar and it has links as menu itemsWhen I click a menu item(link) then link works fine but drop down menu gets closed

51
{select: false} works differently on mongoose queries

{select: false} works differently on mongoose queries

I am trying to create a user in mongoose and return it after Usercreate query without password field

57
How to generate random password in python [duplicate]

How to generate random password in python [duplicate]

I want to use python3 to generate random password to encrypt my files, The random password created should have following restriction

76
Post Image data in android studio to a server url not working on android 4.1.2

Post Image data in android studio to a server url not working on android 4.1.2

I am taking a screenshot on my android app using this code:

75