PHP Generating PDF

Asked

Viewed 880 times

2

Well I have a problem in a form, basically I am taking the information of an HTML form with the POST method for a PHP page, I need to generate a pdf from this form and I managed to do this with mPDF (6.0) until then it is all right, but I can only save 1 pdf, I believe it is because of the file name it is overwriting the file that already exists in the folder.

I wonder if I can create something like an auto increment to avoid files with the same name, or if I can use a variable to take a form field and use it as the 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');

Like I said it’s working but I can only keep 1 file saved.

Thank you in advance.

2 answers

2


To not 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 in the folder.

  • What if two online users submit the form at 06:47:11 of 1°/08/2017?

  • I had forgotten to implement the milliseconds. And I agree that without the milliseconds two requisitions were possible to occur at the same time.

  • Fair and perfect!

  • 2

    It worked perfectly thank you!

2

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.