简体   繁体   English

PHP 表格结果到 Dompdf PDF

[英]PHP Form result to Dompdf PDF

I am trying to export the results of a form generated by PHP to PDF through DOMPDF.我正在尝试通过 DOMPDF 将 PHP 生成的表单的结果导出到 PDF。 But its generating a blank page.但它会生成一个空白页。 May be the generated HTML has some issues.可能是生成的 HTML 有一些问题。

PHP Script is as follows. PHP 脚本如下。 Iam just pasting the HTML output here since the PHP file is long and has lots of functions.我只是在这里粘贴 HTML output 因为 PHP 文件很长并且有很多功能。 so i guess its a problem with HTML formatting:所以我猜这是 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>

THE PHP SCRIPT IS AS FOLLOWS: 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 }  }?>

The problem is that you output HTML content even when the pdf query string exists in the URL.问题是您的 output HTML 内容即使 pdf 查询字符串存在于 ZE6B391A3D2C4D4597026A 中The generated PDF will be mixed with HTML so it becomes invalid.生成的 PDF 会和 HTML 混在一起所以失效。 You shouldn't send any HTML code to the browser when you send the PDF.发送 PDF 时,不应向浏览器发送任何 HTML 代码。 Here is a possible solution with conditionals:这是带有条件的可能解决方案:

<?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 } ?>

You only generate the table when a POST request was made and the data is available to generate the table.您仅在发出 POST 请求并且数据可用于生成表时生成表。 When you click on the PDF link a GET request is made.当您单击 PDF 链接时,会发出 GET 请求。 You can solve it by sending and receiving data with GET instead of POST.您可以通过使用 GET 而不是 POST 发送和接收数据来解决它。 (eg.: $_POST['language'] => $_GET['language']). (例如:$_POST['language'] => $_GET['language'])。 Another solution is to replace the PDF link with a form (it can be an invisible form with hidden input fields) that sends the necessary data.另一种解决方案是将 PDF 链接替换为发送必要数据的表单(它可以是具有隐藏输入字段的不可见表单)。 You should also delete other <form> and </form> tags from the page(I think they are unnecessary).您还应该从页面中删除其他<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>

I hope it helps you!我希望它对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM