我想用一個非常簡單的HTML生成PDF。但是,它包含5個圖像(總共不到50KB)。DOMPDF生成pdf很慢
它正在生成PDF非常慢。生成PDF需要12秒以上的時間。我使用XAMPP在Windows 10(i5-4590)上運行。
下面是我的代碼:
<?php
require_once 'dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new DOMPDF();
$html = <<<EOD
<html>
<head>
<style type="text/css">
html {
margin: 1%;
padding: 0;
}
.header,
.footer {
width: 100%;
text-align: center;
position: fixed;
font-size: 26px;
}
.header {
top: 0px;
}
.footer {
bottom: 18px;
}
.center {
margin: 0 auto;
}
.divTable{
display: table;
width: 100px;
text-align: center;
margin: 0 auto;
}
.divTableRow {
display: table-row;
}
.divTableHeading {
margin: 0 auto;
}
.divTableCell, .divTableHead {
display: table-cell;
padding: 0 33px;
}
.bottom .divTableCell {
padding-top: 30px;
}
.divTableHeading {
display: table-header-group;
font-weight: bold;
}
.divTableFoot {
display: table-footer-group;
font-weight: bold;
}
.divTableBody {
display: table-row-group;
}
div.img-border img {
border-style: solid solid solid solid;
border-width: 2px 2px 2px 2px;
border-color: #eb0089;
}
</style>
</head>
<body>
<div class="center">
<div class="divTable top">
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell" style="padding-left:0px;"><div class="img-border"><img src="300-small.png"></div></div>
<div class="divTableCell"><div class="img-border"><img src="300-small.png"></div></div>
<div class="divTableCell" style="padding-right:0px;"><div class="img-border"><img src="300-small.png"></div></div>
</div>
</div>
</div>
<div class="divTable bottom">
<div class="divTableBody">
<div class="divTableRow">
<div class="divTableCell" style="padding-left:0px;"><div class="img-border"><img src="300-large.png"></div></div>
<div class="divTableCell" style="padding-right:0px;"><div class="img-border"><img src="300-large.png"></div></div>
</div>
</div>
</div>
</div>
<div class="footer">
$customer_title - $customer_order_number
</div>
</body>
</html>
EOD;
$dompdf->set_option('dpi' , '300');
$dompdf->load_html($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("sample.pdf", array("Attachment"=>0));
我不理解如何使過程更快。在的第二個DIV再次
<div class="divTableCell style="padding-right:0px;">
應該
<div class="divTableCell" style="padding-right:0px;">
而且同樣的錯誤:
這些是什麼類型的PNG圖像。過去我看到一些PNG圖像類型(例如灰度),出於未知原因,需要大量的處理時間。另外,您使用PNG的事實意味着Dompdf將執行額外的處理。如果切換到JPG或刪除圖像,處理時間是否會改善? – BrianS
@BrianS我不知道爲什麼它很慢,它是如何修復的,但它現在工作正常。我曾嘗試過許多圖像(PNG和JPG),結果與之前相同。我沒有改變PDF生成代碼的任何內容。但整體項目的代碼變了很多。 –