How to make PHP wait until PDF is created for printed?

1.3k views Asked by At

I have a script that creates a PDF document and proceed to send it to be printed. It happens that I need to wait for the document to be created, check that and recently sent for printing.

    define('TIKET_DIR', public_path('temp/'));
    $token = sha1(microtime().'tk');
    $pdfPath = TIKET_DIR.$token.'.pdf';    
    $html2pdf = new HTML2PDF('V', array('72', '110'), 'es', true, 'UTF-8', 0);
    $html2pdf->WriteHTML($html);
    $html2pdf->Output($pdfPath, 'F');
    $cmd = "lpr -P".$ococina->impresora." ";
    $cmd .= $pdfPath;
    $response = shell_exec($cmd);

The variable $html is dynamically created and may be delayed. Usually PDF document printed, but sometimes it does not print and I think this happens because the lpr command is executed without the PDF document is ready. I use LEMP on Ubuntu with CUPS print server. I have to turn off and turn on all printers connected to the computer and just the PDF document previously sent print.

Thanks in advance.

2

There are 2 answers

1
bklaase On

Well, PHP has a sleep function:

int sleep ( int $seconds )

(see: http://php.net/manual/en/function.sleep.php) But this is not a nice way to solve problems. Also you might run into max execution time as defined by either your webserver, or your PHP settings.

Perhaps look into asynchronous sollutions? For example, process info needed for pdf and e-mail afterwards

0
Pavan Pyati On

Use below code, which works like a charm.

html2pdf().from(element).save().then(function () {
    // Whatever you want to happen when it's done!
});