生成的 dompdf 输出的打印按钮

Print button for generated dompdf output

HTML

<p style="color:red;font-size: 30px;">sfsdfsdfdsfsdfdsfdsf</p>sdgfhgfhgfhg

PHP

<?php
// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

//to put same file html
/*$html1 =
  '<html><body>'.
  '<p>Put your html here, or generate it with your favourite '.
  'templating system.</p>'.
  '</body></html>';*/

// instantiate and use the dompdf class
$dompdf = new Dompdf();

//to put other html file
$html = file_get_contents('index.html');
$dompdf->loadHtml($html);
//$dompdf->loadHtml('<h1>Welcome to CodexWorld.com</h1>');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Legal', 'Landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
//$dompdf->stream();

// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("codex",array("Attachment"=>0));

//$output = $dompdf->output();
//file_put_contents("pdfs/file.pdf", $output);
?>

我正在使用 dompdf 将我的 html 转换为 pdf,我想在这里做的是 index.html 页面中的打印按钮,当我单击该打印按钮时,生成的 pdf 应该下载到用户系统。

我怎样才能做到这一点

这应该有效:

<?php
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

if (isset($_GET['action']) && $_GET['action'] == 'download') {

  // instantiate and use the dompdf class
  $dompdf = new Dompdf();

  //to put other html file
  $html = file_get_contents('index.html');
  $html .= '<style type="text/css">.hideforpdf { display: none; }</style>';
  $dompdf->loadHtml($html);

  // (Optional) Setup the paper size and orientation
  $dompdf->setPaper('Legal', 'Landscape');

  // Render the HTML as PDF
  $dompdf->render();

  // Output the generated PDF (1 = download and 0 = preview)
  $dompdf->stream("codex",array("Attachment"=>1));
}
?>


<a class="hideforpdf" href="generatepdf.php?action=download" target="_blank">Download PDF</a>

编辑:将使用部分移至代码顶部。
编辑 2:添加 class 以隐藏下载按钮。