PHP格式结果为Dompdf PDF

问题描述 投票:0回答:1

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

PHP脚本如下。 Iam只是将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 }  }?>
php dompdf
1个回答
0
投票

问题是,即使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请求且数据可用于生成表时才生成表。当您单击PDF链接时,将发出GET请求。您可以通过使用GET(而不是POST)发送和接收数据来解决此问题。 (例如:$ _POST ['language'] => $ _GET ['language'])。另一个解决方案是将PDF链接替换为发送必要数据的表单(可以是带有隐藏输入字段的不可见表单)。您还应该从页面中删除其他<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>

希望对您有帮助!

© www.soinside.com 2019 - 2024. All rights reserved.