简体   繁体   中英

PHP Form result to Dompdf PDF

I am trying to export the results of a form generated by PHP to PDF through DOMPDF. But its generating a blank page. May be the generated HTML has some issues.

PHP Script is as follows. Iam just pasting the HTML output here since the PHP file is long and has lots of functions. so i guess its a problem with HTML formatting:

<?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
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.The generated PDF will be mixed with HTML so it becomes invalid. You shouldn't send any HTML code to the browser when you send the PDF. 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. When you click on the PDF link a GET request is made. You can solve it by sending and receiving data with GET instead of POST. (eg.: $_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. You should also delete other <form> and </form> tags from the page(I think they are unnecessary).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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