Fidha Nasher Fidha Nasher - 1 month ago 12
AngularJS Question

Download PDF file from Sever through Angularjs Get Method

I am created a lavavel DomPdf using below code segment

$data='<h1>Hai</h1>';
$pdf = App::make('dompdf.wrapper');
$pdf->loadHTML($data);
return $pdf->download('sample.pdf');


It perfectly working with the route and getting PDF.

I need to download it using AngularJs GET Method as given below

var file = new Blob([ data ], {
type : 'application/csv'
});
//trick to download store a file having its URL
var fileURL = URL.createObjectURL(data);
var a = document.createElement('a');
a.href = fileURL;
a.target = '_blank';
a.download = 'yourfilename.pdf';
document.body.appendChild(a);
a.click();


It not working. I need to Download PDF using AngularJs GET Method

Answer

use POST Method rather than GET Method and use the following AngularJs Code

var req = {
        method: 'POST',
        url: 'api/getBulkHallTicket',
        responseType:"arraybuffer",
        data: {exam_centres_id:$scope.option1,mode:$scope.mode}
    };

    $http(req)
        .success(function(data, status, headers){
            var arr = data;
            var byteArray = new Uint8Array(arr);
            var a = window.document.createElement('a');

            a.href = window.URL.createObjectURL(
                new Blob([byteArray], { type: 'application/octet-stream' })
            );
            a.download ='hallticket.pdf' ;//headers('filename');

            // Append anchor to body.
            document.body.appendChild(a);
            a.click();


            // Remove anchor from body
            document.body.removeChild(a);
        }
    ).error(function(){
        }
    );

I hope this will help you.

Comments