PHP Generating PDF

2

Well, I'm having a problem with a form, basically I'm getting the information from an HTML form with the POST method for a PHP page, I need to generate a pdf from that form and I managed to do it with mPDF (6.0) all right, but I can only save 1 pdf, I believe it is because of the filename it is replacing the file that already exists in the folder.

I would like to know if I can create something like an auto increment to avoid files with the same name, or if I can use a variable to get a form field and use it as a name.

Follow the PDF output code with the mPDF class:

$mpdf=new mPDF(); 

$mpdf->SetDisplayMode('fullpage');

$css = file_get_contents("css/estilo.css");

$mpdf->WriteHTML($css,1);

$mpdf->WriteHTML($pdf);

$mpdf->Output('-local-\form.pdf', 'F');

As I said it is working, however, I can only keep 1 file saved.

Thank you in advance.

    
asked by anonymous 01.08.2017 / 14:57

2 answers

2

In order not to overwrite the file, the name must be unique in the folder.

$codigo = date('YmdHis.U');
$arquivo = '-local-\form-' . $codigo . '.pdf';
$mpdf->Output($arquivo, 'F');
//Saída: form-20170801064711.pdf

Note: I try to use the date to know when the file was sent to the folder.

    
01.08.2017 / 15:47
2

Dynamically you can use some of the values submitted by the form as the filename. For example, concatenating name + underscore + cpf: cpf_name.pdf (if this exists in your form and you are sure there will be no repetition of this combination of values). But if any filename is good, let PHP generate one for you, like this:

$mpdf->Output('-local-\'.uniqid().'.pdf', "F"); 
// exemplo: eb98xzzhr8dervre.pdf
    
01.08.2017 / 15:48