2016-11-03 2 views
0

HTMLPrint-Taste erzeugt dompdf Ausgabe

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

ich dompdf bin mit meinem HTML in PDF konvertieren, was will ich hier tun, ist eine Drucktaste in index.html Wenn ich auf diese Druckschaltfläche klicke, sollte das generierte PDF auf das System des Benutzers heruntergeladen werden.

Wie kann ich erreichen, dass

Antwort

0

Diese shoud arbeiten:

<?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: die Verwendung-Teil an die Spitze des Codes bewegt.
Edit 2: eine Klasse hinzugefügt, um die Download-Schaltfläche auszublenden.

+0

Getting Parse-Fehler: Syntaxfehler, unerwarteter 'verwenden' (T_USE) Fehler – vamsi

+0

Verschieben Verwendung Dompdf \ Dompdf; outside isset hat funktioniert, aber kann ich den download button im hinblick auf pdf zeigen? – vamsi

+0

Ich weiß nicht, was Sie genau meinen, aber Sie können den Download-Button überall dort platzieren, wo Sie wollen und auf das richtige Skript mit etwas wie 'Download PDF' verweisen. Sie müssen nicht im selben Skript sein. – FamousWolluf