PHP 7 HTML2PDF Script to Print Data From HTML Post Form as PDF Document in Browser Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

 

index.php

 

 

<?php
if(isset($_POST['sweets'])){

// get the HTML
ob_start();
?>
<page backtop="35mm" backbottom="20mm" backleft="15mm" backright="15mm">
    <div id="p1-content">
        <h3>Mer test</h3>
        <p>Normalement une valeur devrait vous être demandée, puis affichée</p>
    </div>
</page>

<?php
// PDF script to execute
$content = ob_get_clean();
// convert to PDF
require_once(dirname(__FILE__).'/html2pdf/vendor/autoload.php');

try
{
    $html2pdf = new HTML2PDF('P', 'A4', 'en');    
    $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
    $html2pdf->Output('testprint.pdf','D');
}

catch(HTML2PDF_exception $e) {
    echo $e;
    exit;
}
?>
<?php
} else {
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $(".sendData").click(function () {
        var str = "";
        $("select[name='sweets'] option:selected").each(function () {
            str += $(this).text() + " ";
        });
        jQuery.ajax({
            type: "POST",
            data:  $("form#a").serialize(),
            success: function(data){
                jQuery(".res").html(data);
                $('#test').html(data);
            }
        });
        var str = $("form").serialize();
        $(".res").text(str);
    });
});
</script>
<div id="test"></div>
<form id="a" action="" method="post">
    <select name="sweets" >
        <option>Chocolate</option>
        <option selected="selected">Candy</option>
        <option>Taffy</option>
        <option>Caramel</option>
        <option>Fudge</option>
        <option>Cookie</option>
    </select>
</form>
<button class="sendData">Send Data</button>
<?php
}
?>

Leave a Reply