2016-11-03 4 views
0

HTMLкнопку печати для вывода генерируется DOMPDF

<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-файл должен загружаться в систему пользователя.

Как я могу добиться того, что

ответ

0

Это Шоуда работы:

<?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> 

Edit: переместил потребительскую часть в верхней части кода.
Редактировать 2: добавлен класс, чтобы скрыть кнопку загрузки.

+0

Получение ошибки Parse: ошибка синтаксиса, непредвиденная ошибка 'use' (T_USE) – vamsi

+0

Перемещение для использования Dompdf \ Dompdf; внешний isset работал, но могу ли я показать кнопку загрузки с учетом pdf? – vamsi

+0

Я не знаю, что вы точно имеете в виду, но вы можете поместить кнопку загрузки везде, где хотите, и ссылаться на правильный скрипт с чем-то вроде 'Download PDF'. Им не обязательно быть в одном скрипте. – FamousWolluf