I'm using the PHP DOMPDF class to generate accounts with dynamic filters.
The user selects the filters in a form and asks to generate, so far so good. But it will have the option of exporting what it has filtered into PDF.
Today, I have the following jQuery code:
$('#exportPDF').click(function(){
var relatorio = $('.relatorios').html();
$.ajax({
type: "POST",
url: "acao.php?acao=exportPDF",
data: 'relatorio='+relatorio,
cache: false,
beforeSend: function() {
$('#loader').fadeIn(500);
},
complete: function() {
$('#loader').fadeOut(500);
},
success: function (retorno) {
console.log(retorno);
$('.relatorios').html(retorno);
// RETORNA MENSAGEM DE SUCESSO
$.gritter.add({
title: 'Sucesso',
text: 'O download irá iniciar em breve.',
class_name: 'success'
});
},
error: function(data) {
console.log(data);
}
});
});
And I have the following code in PHP:
// Inclui classe DOMPDF
require_once("../../../include/class/dompdf/dompdf_config.inc.php");
// Recebe a tabela
$relatorio = $_POST['relatorio'];
$dompdf = new DOMPDF();
$dompdf->load_html($relatorio);
$dompdf->render();
$dompdf->stream('relatorio_cliente.pdf');
The idea would be that when generating the PDF by PHP Ajax would send the download request to the browser / client and the entire process flow. I've tried using the headers in PHP and Ajax itself, among many other things. I have also searched for this on the internet and I have already encountered some problems that are just like mine, I just did not understand it very well.
Can you do this?