Parse error: syntax error, unexpected ‘use’ (T_USE) in C:wampwwwcalculaterwp-contentthemescalculaterpage.php on line

ob_start();  
require_once 'dompdfautoload.inc.php';

use DompdfDompdf;

 //use DompdfDompdf;

// instantiate and use the dompdf class
$dompdf = new DOMPDF();
$html = "
print_r($_POST);
";

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents("page.pdf", $pdf);

?>  
<a href="./page.pdf" download="page.pdf">Download the pdf</a>
   <?php
exit;
?>

I try to do downloadable PDF script but getting parse error.

Related posts

1 comment

  1. You have problem with use of use🙂

    The use keyword must be declared in the outermost scope of a file (the
    global scope) or inside namespace declarations. This is because the
    importing is done at compile time and not runtime, so it cannot be
    block scoped.

    Try this code:

    use DompdfDompdf;
    
    ob_start();  
    require_once 'dompdfautoload.inc.php';
    
    // instantiate and use the dompdf class
    $dompdf = new DOMPDF();
    $html = "
    print_r($_POST);
    ";
    
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render();
    $pdf = $dompdf->output();
    file_put_contents("page.pdf", $pdf);
    
    ?>  
    <a href="./page.pdf" download="page.pdf">Download the pdf</a>
       <?php
    exit;
    ?>
    

Comments are closed.