Why does not my app made in laravel 5.4 generate the pdf with barryvdh / laravel-dompdf 8.0?

3

I just have this problem.

I install the package correctly and all right up there, but I try to generate the pdf with barryvdh / laravel-dompdf , but all my attempts have been unsuccessful.

My ajax:

$.ajax({
    type: 'post',
    url: 'equipos/generarPdf',
        global: false,
    data: {
        'data': data
    },
}).then(
    function(data) {
        console.log("Documento Generado");
    }, function(data) {
        console.log('this will run if the $.ajax fails');
    }, function() {
        console.log('this will run if the deferred generates a progress update');
    }
);

Note: in the execution of the ajax returns me that there was no problem.

My route:

Route::post('equipos/generarPdf','EquiposController@generarPdf');

Note: As for the route, it recognizes me well.

My driver (function generatePdf):

public function generarPdf(Request $req) {
    $pdf = \App::make('dompdf.wrapper');
    $pdf->loadHTML('Test');
    return $pdf->stream();
}

Note: When you arrive here, my app does nothing and I check in network of Google Chrome and it returns the following:

Update 1:

I tried different ways:

Here I will upload the file and execute stream :

public function generarPdf(Request $req) {
    $datos = $req->data;
    $name_file = $datos[1].'-'.$datos[2].'.pdf';
    $file_storage = storage_path('app\public\pdf\equipos\').$name_file;
    $file_public = public_path('storage\pdf\equipos\').$name_file;

    $pdf = \PDF::loadView('equipos.pdf_vista', $datos);
    $pdf->setPaper('a4');
    $pdf->setWarnings(false);
    $pdf->save($file_storage);
    return $pdf->loadFile($file_public)->stream($name_file);
}

Here I run stream :

public function generarPdf(Request $req) {
    $datos = $req->data;
    $name_file = $datos[1].'-'.$datos[2].'.pdf';
    $file_storage = storage_path('app\public\pdf\equipos\').$name_file;
    $file_public = public_path('storage\pdf\equipos\').$name_file;

    $pdf = \PDF::loadView('equipos.pdf_vista', $datos);
    $pdf->setPaper('a4');
    $pdf->setWarnings(false);
    $pdf->save($file_storage);
    return $pdf->stream($name_file);
}

Here is the file:

public function generarPdf(Request $req) {
    $datos = $req->data;
    $name_file = $datos[1].'-'.$datos[2].'.pdf';
    $file_storage = storage_path('app\public\pdf\equipos\').$name_file;
    $file_public = public_path('storage\pdf\equipos\').$name_file;

    $pdf = \PDF::loadView('equipos.pdf_vista', $datos);
    $pdf->setPaper('a4');
    $pdf->setWarnings(false);
    $pdf->save($file_storage);
    return $pdf->download($name_file);
}

NOTE: the file is created in the folder publica of laravel but does not generate the download of the file, for the 3 options I do not get an error, but rather in response I get the same as the image above.

    
asked by Pablo Contreras 22.06.2017 в 17:41
source

3 answers

1

If you already have PDF symbols in the console log, the only thing you have wrong is your ajax. Use this:

$.ajax({                 
         type: 'post',
         url: 'equipos/generarPdf',
         global: false,
         data: {
           'data': data
         }
         //,dataType: "text",
         //,async: false
        }).done(function (data, textStatus, jqXHR) {
                console.log(ruta);
                var nombreLogico = "XXX";
                var parametros = "dependent=yes,locationbar=no,scrollbars=yes,menubar=yes,resizable,screenX=50,screenY=50,width=850,height=1050";
                var htmlText = "<embed width=100% height=100% type='application/pdf' src='data:application/pdf," + escape(data) + "'></embed>";
                var detailWindow = window.open("", nombreLogico, parametros);
                detailWindow.document.write(htmlText);
                detailWindow.document.close();
        }).fail(function (jqXHR, textStatus, errorThrown) {
            alert("error\njqXHR=" + jqXHR + "\nstatus=" + textStatus + "\nerror=" + errorThrown);
        }).always(function(dataORjqXHR, textStatus, jqXHR_ORerrorThrown) {
            //alert( "complete" );
        });

If it still does not work, check in that console.log that the PDF symbols do not show strange characters. If they appear, you have to try combinations of utf8 encode and utf8 decode on the javascript and php side until it works.

    
answered by 26.06.2017 / 17:16
source
1

Make sure that your pdf is being generated correctly, creating a GET route and accessing from the browser. Then try this.

var oReq = new XMLHttpRequest();
oReq.open("POST", "equipos/generarPdf", true);
oReq.responseType = "arraybuffer";

oReq.onload = function(oEvent) {
    if (oReq.readyState === 4) {  
        if (oReq.status === 200) {  
            console.log(oReq.responseText)  
        } else {  
            console.log("Error", oReq.statusText);  
        }  
    }
};

oReq.send();

[EDITED]

  var oReq = new XMLHttpRequest();
    oReq.open("POST", "equipos/generarPdf", true);
    oReq.responseType = "arraybuffer";

    oReq.onload = function(oEvent) {
        var arrayBuffer = oReq.response;
        if (oReq.readyState === 4) {  
            if (oReq.status === 200) {  
                var blob = new Blob([arrayBuffer], {type: "application/pdf"});
                var objectUrl = URL.createObjectURL(blob);
                window.open(objectUrl);
            } else {  
                console.log("Error", oReq.statusText);  
            }  
        }
    };

    oReq.send();
    
answered by 26.06.2017 в 00:53
0

I recently had a problem with it ... and it was my version of php it turns out that in my case the laragon that replaces the xampp was with version php 7.0 so you can check the documentation of dompdf and also see what version of php you are managing and if er greater then you will not be able to change the version of php or create the project with a smaller version of php the one that says in dompdf

    
answered by 23.06.2017 в 19:46