I created a directory within / public called / uploads / pdf. In this directory I save PDF files that I need to download. I created an AJAX request for this which calls an application controller for this, however, via AJAX it does not work.
If I make the call, it usually works.
See the code:
PDFClass.php // Class I created globally registered
public static function createPDF($view){
define('BUDGETS_DIR', public_path('uploads/pdf'));
if (!is_dir(BUDGETS_DIR)){
mkdir(BUDGETS_DIR, 0755, true);
}
$outputName = str_random(10);
$pdfPath = BUDGETS_DIR.'/'.$outputName.'.pdf';
File::put($pdfPath, PDF::load($view, 'A4', 'portrait')->output());
}
As you can see, he already has the call to download the file.
In the controller I call this method to create the report and return.
ReservationController.php
PDFClass::createPDF(HTMLRelatorio::confirmacaoReserva($fileServico,$motorista));
That creates the PDF file, saves it in the specified directory, but ... Does not open the download window because the call is by AJAX ...
The call in view: reserved.blade.php
$(this).on('click','#imprimirConfirmacao',function(){
var idFile = $('#id_file').val();
var idServ = $('#id_servico').val();
$.ajax({
url: '/reserva/imprimir',
type: 'POST',
dataType: 'html',
data: {idFile: idFile,idServ:idServ},
success: function(data){
console.log(data);
}
});
Could you help me with this. If someone can pass me a way to download the file via ajax or a way to access the absolute path to create a link by Laravel I thank you, because if I make the call by the direct browser also returns error:
See:
Returns error:
Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException