PHP 表单结果转为 Dompdf PDF

PHP Form result to Dompdf PDF

我正在尝试通过 DOMPDF 将 PHP 生成的表单结果导出为 PDF。但它生成一个空白页。可能是生成的 HTML 有一些问题。

PHP脚本如下。我只是在这里粘贴 HTML 输出,因为 PHP 文件很长并且有很多功能。所以我猜这是 HTML 格式的问题:

<?php 
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
?>
<!DOCTYPE html>
<head>
<title>Country Language</title>
</head>
<body>

<?php ob_start(); ?>
<form>
<table border="1"><tr><td><b>Countries Selected</b></td><td><b>Details</b></td><td><b>Amount</b></td></tr>  
    <tr>
        <td rowspan='8'><h2>Australia</h2></td>
    </tr>
    <tr>
        <td>&nbsp;Z</td>
        <td>3</td>
    </tr>
        <tr>
        <td>&nbsp;Entity</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;N</td>
        <td>&nbsp;0</td>
    </tr>
    <tr>
        <td>English to English</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;X</td>
        <td>&nbsp;1</td>
    </tr>
    <tr>
        <td>&nbsp;Y</td>
        <td>&nbsp;2</td>
    </tr>
    <tr>
        <td><strong>TOTAL PATENT FEE</strong></td>
        <td><b>&nbsp;USD&nbsp;1400.45</b></td>
    </tr>
    <tr>
        <td rowspan='8'><h2>India</h2></td>
    </tr>
    <tr>
        <td>&nbsp;Z</td>
        <td>3</td>
    </tr>
        <tr>
        <td>&nbsp;Entity</td>
        <td>&nbsp;Micro</td>
    </tr>
    <tr>
        <td>&nbsp;N</td>
        <td>&nbsp;1</td>
    </tr>
    <tr>
        <td>Arabic to English</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;X</td>
        <td>&nbsp;1</td>
    </tr>
    <tr>
        <td>&nbsp;Y</td>
        <td>&nbsp;2</td>
    </tr>
    <tr>
        <td><strong>TOTAL PATENT FEE</strong></td>
        <td><b>&nbsp;USD&nbsp;315.45</b></td>
    </tr>
</table>



<?php
if ( isset( $_GET['pdf'] ) ) {
  $html = ob_get_contents();
  $html = ob_get_clean();
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->setPaper('A4', 'portrait');
  $dompdf->render();
  $dompdf->stream("the_inquiry.pdf");
}
?>
<div class="shouldPrintToPDF">
    <a  href="/general/cean/cost-calc5.php?&amp;pdf=1" class="pdflink">Open as PDF</a>
  </div>
</form>
</body>
</html>

PHP 脚本如下:

<?php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;


    if (isset($_GET['pdf'])) {
    ob_start(); 
}
else {
?>

<!DOCTYPE html>
<head>
<title>Country Language</title>
</head>
<body>
<form>



<?php

if(isset($_POST['submit'])) {
$language_selected = $_POST['language'];
$claims_selected = $_POST['claims'];
$pages_selected = $_POST['pages'];
$totalwords_selected = $_POST['totalwords'];
$claims_selected = $_POST['claims'];
$entity_selected1 = $_POST['entity_selected'];


//echo '<div class="container">';
echo '<form>';

        echo '<table border="1">';
        echo '<tr><td><b>Countries Selected</b></td><td><b>Fee Details</b></td><td><b>Amount</b></td></tr>';

            foreach ($_POST['check_list'] as $key => $country_checked) {

                if(!empty($_POST['entity_selected'][$key])) {
                $entity_selected = $_POST['entity_selected'][$key];
                } else {
                $entity_selected = '';
                }

            $sql_db = mysqli_query($conn,"SELECT id, country, language, entity FROM country_languages where country = '$country_checked' ");
            //$row_db = mysqli_fetch_assoc($sql_db);
                while($row_db = mysqli_fetch_array($sql_db)) {
                    $id = $row_db["id"];
                    $country = $row_db["country"];
                    $language = $row_db["language"];
                    $entity = $row_db["entity"];
                        if ($language_selected == $language) {
                         $Z = '0';
                        } else {
                         $Z = $totalwords_selected;
                        }
                             if ($entity_selected =='') {
                                $N = '0';
                             } else if ($entity_selected == 'Micro') {
                                $N = '1';
                             } else if (($entity_selected == 'Small') AND ($country_checked == 'India')) {
                                $N = '2.2';
                             }  else if (($entity_selected == 'Small') AND ($country_checked !== 'India')) {
                                $N = '2';
                             } else if (($entity_selected == 'Large') AND ($country_checked == 'India')) {
                                $N = '4.4';
                             } else if (($entity_selected == 'Large') AND ($country_checked == 'Philippines')) {
                                $N = '5.5';
                             } else if (($entity_selected == 'Large') AND ($country_checked !== 'Philippines') AND ($country_checked !== 'India')) {
                                $N = '4';
                             } else {
                                $N = '0';
                             }
                            $X = $claims_selected;
                            $Y = $pages_selected;
                        if ($country_checked == 'USA') {
                        $result = (430 + 25 * ($X - 20) + 200 * (($Y - 100) / 50 + (1 - ($Y - 100) / 50))) * $N + 0.15 * $Z + 750;  
                        } else {
                        $result = 'Not able to fetch data.';
                        }

?>
    <tr>
        <td rowspan='8'><h2><?php echo $country_checked; ?></h2></td>
    </tr>
    <tr>
        <td>&nbsp;Z</td>
        <td><?php echo $Z; ?></td>
    </tr>
        <tr>
        <td>&nbsp;Entity</td>
        <td>&nbsp;<?php echo $entity_selected; ?></td>
    </tr>
    <tr>
        <td>&nbsp;N</td>
        <td>&nbsp;<?php echo $N; ?></td>
    </tr>
    <tr>
        <td><?php echo $language_selected; ?> to <?php echo $language; ?></td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;X</td>
        <td>&nbsp;<?php echo $X; ?></td>
    </tr>
    <tr>
        <td>&nbsp;Y</td>
        <td>&nbsp;<?php echo $Y; ?></td>
    </tr>
    <tr>
        <td><strong>TOTAL PATENT FEE</strong></td>
        <td><b>&nbsp;USD&nbsp;<?php echo number_format((float)$result, 2, '.', ''); ?></b></td>
    </tr>
<?php
}   // END WHILE LOOP   
    } //END FOR LOOP
echo '</table>';
}

?>

<?php
if (isset($_GET['pdf'])) {
  $html = ob_get_clean();
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->setPaper('A4', 'portrait');
  $dompdf->render();
  $dompdf->stream("the_inquiry.pdf");
} 

else {
?>
<div class="shouldPrintToPDF">
<a href="<?php echo $_SERVER['PHP_SELF'] , '?' , http_build_query( $_GET ); ?>&amp;pdf=1" class="pdflink">Open or save as PDF</a>

</div>
</form>
</body>
</html>

<?php }  }?>

问题是即使 URL 中存在 pdf 查询字符串,您也会输出 HTML 内容。生成的 PDF 将与 HTML 混合,因此它变得无效。发送 PDF 时,不应向浏览器发送任何 HTML 代码。这是一个可能的条件解决方案:

<?php 
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;

if (isset($_GET['pdf'])) {
    ob_start(); 
}
else {
?>
<!DOCTYPE html>
<head>
<title>Country Language</title>
</head>
<body>
<form>
<?php } ?>
<table border="1"><tr><td><b>Countries Selected</b></td><td><b>Details</b></td><td><b>Amount</b></td></tr>  
    <tr>
        <td rowspan='8'><h2>Australia</h2></td>
    </tr>
    <tr>
        <td>&nbsp;Z</td>
        <td>3</td>
    </tr>
        <tr>
        <td>&nbsp;Entity</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;N</td>
        <td>&nbsp;0</td>
    </tr>
    <tr>
        <td>English to English</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;X</td>
        <td>&nbsp;1</td>
    </tr>
    <tr>
        <td>&nbsp;Y</td>
        <td>&nbsp;2</td>
    </tr>
    <tr>
        <td><strong>TOTAL PATENT FEE</strong></td>
        <td><b>&nbsp;USD&nbsp;1400.45</b></td>
    </tr>
    <tr>
        <td rowspan='8'><h2>India</h2></td>
    </tr>
    <tr>
        <td>&nbsp;Z</td>
        <td>3</td>
    </tr>
        <tr>
        <td>&nbsp;Entity</td>
        <td>&nbsp;Micro</td>
    </tr>
    <tr>
        <td>&nbsp;N</td>
        <td>&nbsp;1</td>
    </tr>
    <tr>
        <td>Arabic to English</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td>&nbsp;X</td>
        <td>&nbsp;1</td>
    </tr>
    <tr>
        <td>&nbsp;Y</td>
        <td>&nbsp;2</td>
    </tr>
    <tr>
        <td><strong>TOTAL PATENT FEE</strong></td>
        <td><b>&nbsp;USD&nbsp;315.45</b></td>
    </tr>
</table>



<?php
if (isset($_GET['pdf'])) {
  $html = ob_get_clean();
  $dompdf = new DOMPDF();
  $dompdf->load_html($html);
  $dompdf->setPaper('A4', 'portrait');
  $dompdf->render();
  $dompdf->stream("the_inquiry.pdf");
}
else {
?>
<div class="shouldPrintToPDF">
    <a href="/general/cean/cost-calc5.php?&amp;pdf=1" class="pdflink">Open as PDF</a>
</div>
</form>
</body>
</html>

<?php } ?>

您仅在发出 POST 请求且数据可用于生成 table 时生成 table。当您单击 PDF link 时,会发出 GET 请求。您可以通过使用 GET 而不是 POST 发送和接收数据来解决它。 (例如:$_POST['language'] => $_GET['language'])。另一种解决方案是将 PDF link 替换为发送必要数据的表单(它可以是具有隐藏输入字段的不可见表单)。您还应该从页面中删除其他 <form></form> 标签(我认为它们是不必要的)。

<?php
function repopulate($field) {

    if (isset($_POST[$field])) {
        echo htmlspecialchars($_POST[$field]);
    }
}
?>
<div class="shouldPrintToPDF">
    <form method="post" action="?pdf=1">
        <input type="hidden" name="language" value="<?php repopulate('language'); ?>">
        <!-- Add all your fields (claims, pages...) here and use the repopulate function -->
        <button name="submit" type="submit">Open or save as PDF</button>
    </form>
</div>

希望对您有所帮助!