Print query in a PDF with HTML2PDF (jquery ajax)

1

I will try to explain myself as best I can. What I'm looking for is printing a table, which is the result of a query I get with jquery ajax, in a PDF file. The query is made from these data:

The user chooses and when clicking on the query, I get the query with jquery ajax, the code is as follows:

$.ajax({
    url: 'vistas/modulos/secundaria.php',
    method: 'POST',
    data: parametros,
    dataType: 'json',
    cache: false,
    contentType: false,
    processData: false,
    success: function(registros){

        var opcion;
        var entrega;
        var costo;
        var auxiliar = 0;

        $.each(registros, function(i, registro){

            if (typeof (registro.primer_aviso) != "undefined") {
                opcion = registro.primer_aviso;
                entrega = registro.entrega_primer;
                costo = registro.costo_primer;
            }
            else{
                opcion = registro.testimonio;
                entrega = registro.entrega_testimonio;
                costo = registro.costo_testimonio;
            }

            auxiliar = parseInt(auxiliar) + parseInt(costo);

            $('#consultaSecundaria').append(
                '<tr>'+
                    '<td>'+registro.municipio+'</td>'+
                    '<td>'+registro.escritura+'</td>'+
                    '<td>'+registro.enajenante+'</td>'+
                    '<td>'+registro.adquiriente+'</td>'+
                    '<td>'+opcion+'</td>'+
                    '<td>'+entrega+'</td>'+
                    '<td>'+costo+'</td>'+
                '</tr>'
            );
        });

    }
});

With this code I get a result like that, until here I have no problem:

Now what I need, is that when the user clicks on the CREATE PDF button, a new tab should be opened with the resulting table of the query in a pdf file, in fact I can print it in a pdf file, my problem is that I can only send print without styles. So:

What I did was save all the contents of the table in a variable, like this:

$('#pdf').on('click', function() {

    var auxiliar = $('#consultaSecundaria').html();
    var contenido = "<table border='1'>"+auxiliar+"</table>";
    //contenidoPdf(contenido);
    window.open ("vistas/modulos/pdf.php?contenido="+contenido, '_blank');

});

And then in the php file this:

<?php 

require '../../vendor/autoload.php';
use Spipu\Html2Pdf\Html2Pdf;

$imprimir = $_GET['contenido'];

$objeto = new Html2Pdf();
$objeto -> writeHTML($imprimir);
$objeto -> output('archivo.pdf');

?>

What I learned about HTML2PDF I learned from this video: link

From what I see, to be able to style the pdf file you need a new file in html, which by means of the php functions ob_start () and ob_get_clean () you save it in a variable with all its css styles and then if, You send it to print. My problem is that the table that I want to print in pdf I get from a jquery ajax query, and I can not get it in the same way they do in the tutorials. I do not know how to combine these 2 situations.

I can not think of a way to solve it. Nobody?

    
asked by Oscar Díaz 17.01.2018 в 19:07
source

1 answer

0

what you want to do is load that table into an html with your styles. To not change much what you have done. I give you a json that is the one you draw, you can make another ajax call with that json that returns the table to you with styles and then print it. I have used it several times and it works.

url: 'vistas/modulos/secundaria.php',
method: 'POST',
data: parametros,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
success: function(registros){
 //aqui la llamada  
    $.ajax({
        async: true,   // this will solve the problem
        type: "POST",
        url: DONDE MAQUETAS EL JSON
        contentType: "html",
        success:function (html) {
            AQUI IMPRIMES EL html
        }

    });
    
answered by 30.01.2018 в 14:56