1
ob_start();  
require_once '\dompdf\autoload.inc.php';

use Dompdf\Dompdf;

 //use Dompdf\Dompdf;

// 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.

alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Piyush Trivedi
  • 97
  • 1
  • 1
  • 8

1 Answers1

7

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 Dompdf\Dompdf;

ob_start();  
require_once '\dompdf\autoload.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;
?>
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
  • Is this full text of your php file? What error do you have this time? – alexander.polomodov Mar 21 '16 at 12:34
  • $file_location = $_SERVER['DOCUMENT_ROOT']."pdf/".$pdf.".pdf"; file_put_contents($file_location,$pdf); is not working pdf save to all time different name Warning: file_put_contents() expects parameter 1 to be a valid path, – Piyush Trivedi Mar 21 '16 at 12:50