Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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'];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...