Send data from JavaScript to PHP, for use in FPDF

0

Good afternoon, I have a problem that already has me head, no matter how hard I try to solve it, I have an HTML, which I want to send data to a PHP, so that the latter process it and with the FPDF class creates a PDF file, the data will come from some controls in HTML, that's why I will use JavaScript to send this data to PHP. When I execute it, it just sends a lot of characters to my page and does not create the PDF, is there a way to solve that problem? My code is as follows:

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>              
    </head>
    <body> 

        <script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>
        <script src="main.js"></script>

        <button type="button" onclick="GenerarPDF()">click</button>                    
        <div id="resulta"></div>

    </body>

</html>

main.js

function GenerarPDF() {

    $.ajax(
            {
                type: 'POST',
                url: 'pdf.php',
                data: {"saludo": "hola"}, //aquí le pasaré datos de controles de HTML

                success: function (result) {
                    $('#resulta').html(result);
                }
            }
    );
}

pdf.php

<?php

require_once('PDF/fpdf/fpdf.php');

$saludo = $_POST['saludo'];


$pdf = new FPDF('P', 'mm', 'Letter');
$pdf->AddFont('Courier', '');
$pdf->AddPage();
$pdf->SetFont('Courier', '', 11);
$pdf->Cell(50,10,$saludo);
$pdf->Output();

Thank you in advance to everyone.

P. D. Working with Windows 7 and PHP7.

    
asked by Carlos Daniel Zárate Ramírez 22.06.2018 в 19:15
source

1 answer

0

The characters that you return are the encoding of the PDF: To avoid this you have to add the parameters to output

$pdf->Output("F","..tu ruta de archivo con nombre");

F is an FPDF property that tells the class to save the file locally, then it's the file name, where you put the path where you want to save it.

The methods allowed are:

I: envía el fichero al navegador de forma que se usa la extensión (plug in) si está disponible.
D: envía el fichero al navegador y fuerza la descarga del fichero con el nombre especificado por name.
F: guarda el fichero en un fichero local de nombre name.
S: devuelve el documento como una cadena.

Documentation

    
answered by 16.01.2019 в 04:12