What I am trying to do is that the user clicking the generate button of a form, generate a .pdf
per Ajax request, that is, I have three pages, where Index.php
is the form, controller.php
is the ajax script where to send the data to generate the. pdf
on page procTermo.php
. I do not know if this is possible or I'm doing something wrong. Note: To generate PDF
I use DOMPDF
.
index.php
<form id="termo1" action="controller.php" method="POST" target="_blank">
<div class="panel-body">
<div class="form-group">
<label class="col-xs-3 control-label">Serial:</label>
<div class="col-xs-3">
<input type="text" class="form-control" name="serialTermo1" required/>
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Objeto:</label>
<div class="col-xs-6">
<textarea class="form-control" rows="5" name="objTermo1"></textarea>
</div>
</div>
<div class="cleanfix"></div>
<center><button class="btn btn-default" name="btnGerar" value="gerar1">Gerar <span class="glyphicon glyphicon-save"></span></button></center>
</div>
controller.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script><scripttype="text/javascript">
var data_to_send = JSON.parse('<?php echo json_encode($_POST);?>');
$.ajax({
type: 'POST',
url: 'procTermo.php',
data: data_to_send,
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
</script>
procTermo.php
switch ($_POST['btnGerar']) {
case 'gerar1':
$content = "
<div id='title'><u><b>Capa Termo</b></u></div></center>
<div id='content'><p><b>Serial</b>: ".$_POST['serialTermo1']."</p><p>
<p><b>OBJETO</b>: ".$_POST['$objTermo1']."</p></div></body></html>
";
$dompdf->load_html($content);
$dompdf->render();
$dompdf->stream("saida-termo1.pdf",array("Attachment" => false));
break;