Hôm nay tôi sẽ giải thích cách chuyển đổi html sang pdf trong php bằng cách sử dụng dompdf. Ví dụ này rất dễ sử dụng trong php. Ví dụ này tôi đang sử dụng cho dompdf và tạo tệp html để xem pdf và tải xuống tệp pdf bằng php
Vì vậy, trước tiên hãy tải xuống dompdf được tạo trong thư mục của bạn. Vì vậy, hãy bắt đầu với ví dụ
mục lục. php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
// Load HTML content
// $dompdf->loadHtml('
Welcome to niceshipest.com');// Load html file
$html = file_get_contents("index_pdf.html");
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("niceshipest", array("Attachment" => 0));
?>
Xem Pdf
Chỉ mục_pdf. html
Convert HTML To PDF In PHP With Dompdf
Pdf File Generate To Niceshipest.com
Bây giờ bạn có thể tự kiểm tra
Tôi hy vọng nó có thể giúp bạn
đầu ra
Nếu bạn muốn sử dụng thư viện Dompdf để chuyển đổi HTML sang PDF, thì bạn có thể tải xuống phiên bản mới nhất của thư viện này từ liên kết này của GitHub. Nếu bạn đã tải xuống phiên bản mới nhất của thư viện này thì bạn không muốn tải xuống thư viện PHP khác như thư viện Phông chữ PHP và Thư viện PHP SVG. Cả hai thư viện sẽ đi kèm với phiên bản mới nhất. Bạn có thể tìm thấy hướng dẫn đầy đủ về cách sử dụng thư viện Dompdf từ nguồn bên dưới
Tải xuống mã nguồn
Mã nguồn
<?php //index.php //include autoloader require_once 'dompdf/autoload.inc.php'; // reference the Dompdf namespace use Dompdf\Dompdf; //initialize dompdf class $document = new Dompdf(); $html = ' <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> <table> <tr> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr> </table> '; //$document->loadHtml($html); $page = file_get_contents("cat.html"); //$document->loadHtml($page); $connect = mysqli_connect("localhost", "root", "", "testing1"); $query = " SELECT category.category_name, product.product_name, product.product_price FROM product INNER JOIN category ON category.category_id = product.category_id "; $result = mysqli_query($connect, $query); $output = " <style> table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; } </style> <table> <tr> <th>Category</th> <th>Product Name</th> <th>Price</th> </tr> "; while($row = mysqli_fetch_array($result)) { $output .= ' <tr> <td>'.$row["category_name"].'</td> <td>'.$row["product_name"].'</td> <td>$'.$row["product_price"].'</td> </tr> '; } $output .= '</table>'; //echo $output; $document->loadHtml($output); //set page size and orientation $document->setPaper('A4', 'landscape'); //Render the HTML as PDF $document->render(); //Get output of generated pdf in Browser $document->stream("Webslesson", array("Attachment"=>0)); //1 = Download //0 = Preview ?>
- Chia sẻ cái này.
- Google+
- Tình cờ gặp
- đào