HTML2PDF 如何将 id 传递给要转换的 html 站点

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

我已经开始使用 HTML2PDF (https://www.html2pdf.fr/) 创建发票,现在以下代码可以工作了:

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();
            }

fa.php 的内容被转换为 pdf,但是如果我将其中一行更改为:

include('../faktura/fa.php?id=105');

注意我只添加了?id=105,它returns只是一个空白页。 php 文件 fa.php 包括:

 $id = (int) $_GET['id'];

我需要做的是将 ID 传递给该 php 脚本,以便生成准确订单的发票。

Include 基本上意味着“将代码从那个文件复制到这里”。

因此,如果您将包含的文件代码更改为 $id = (int) $_GET['id'];至 $id = $thatId;

在 include() 函数之前,您需要编写 $thatId = 105; 包含的文件将能够访问该变量。

文件A:

$id = 105;
include('../faktura/fa.php');

文件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

或者,如果您希望 fa.php 继续使用 QUERY PARAMETER 以使用您的发票 (2pdf) 代码:

if(!isset($id) && isset($_GET['id'])) {
  $id = (int) $_GET['id'];
}