6
I am using PHP’s DOMPDF class to generate reports of accounts receivable with dynamic filters.
The user selects the filters in a form and asks to generate, so far so good. But he will have the option to export what he filtered in 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 send to the browser/client the download request and the whole process flow. I’ve tried using headers in PHP and Ajax, among many other things. I’ve also looked for it on the internet and I’ve found some problems like mine, I just didn’t understand it very well.
You can do it?
I thought on this issue, the problem is that over time the server would fill with saved files unless I created a function to erase all X files in X days. What is your opinion?
– Matheus Jordan
You can schedule a task on the server, running every day and deleting files older than 1 day, or something like that.
– bfavaretto
Yes, perfect. I’ll take your suggestion, thanks @bfavaretto
– Matheus Jordan