Generate PDF by Ajax

Asked

Viewed 774 times

0

What I am trying to do is for the user to click the button generate of a form, manages a .pdf by Ajax request, ie I have three pages, where the Index.php this form, the controller.php this is the ajax script where to send the data to generate the .pdf on the page procTermo.php. I do not know if this is possible or I am doing something wrong. Note: To generate the PDF use the 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>

php controller.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="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;
  • Do a test, in Success put this code: var popup= window.open("data:application/pdf;Base64, " + data, '', 'height=650,width=840');

  • Thus -> Success: Function(data){var popup=window.open("data:application/pdf;Base64, " + data, '', 'height=650,width=840');}, when executing the code a pop-up but empty is displayed. Analyzing the request with the firebug, it appears status 200 Ok

  • the request Voce makes for 'procTermo.php' is returning the pdf stream?

  • No, the answer is Reload the page to get source for: http://127.0.0.1/Sis/procTermo.php, now I don’t know if it’s getting to procTermo.php the content of Ajax.

  • When I have to do something like this, I make the form give Submit with target _Blank and on the page that receives the post the pdf, fpdf use.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.