短
我想要做的是準備PHP生成的結果打印與強大的規則。在PHP中使用PDF
嘗試所有可能的方式與CSS + HTML:設置尺寸在PX,毫米,釐米。 沒有幫助。每個瀏覽器,甚至每臺打印機都打印出完全不同的紙張結果(使用&(不帶邊框打印)也嘗試過,也沒有得到結果)。經過長時間的研究,發現CSS不是實現此目的和更好的方式的最佳途徑 - 使用PHP創建PDF創建功能。所以,安裝TCPDF。但是無法使用我爲HTML輸出創建的邏輯部分來工作。
我想從上邊距和紙底雙方必須11毫米
- 表的第一行和最後一行0毫米
- 表的什麼行必須在4毫米從左右紙邊
- 2毫米每列
- 38毫米寬×21.2毫米高度每個小區
- 13 x行,5×列之間服務,13x5 = 65個細胞
- 在新的頁面中的每個表。換句話說 - 每個表分頁符
- 在每個小區中39碼條碼後(值必須是
$id
) - 在PDF結果只有表 - 沒有頁眉,頁腳沒有,沒有標題...等
這裏有更詳細的解釋上的圖像:
什麼我越來越
表單提交後,在PHP端處理需要很長的時間 - 大約一分鐘,打開空白頁而不是PDF結果。
代碼:
(代碼也不是那麼巨大,評論使它看起來像這樣:)
<?php
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('John Smith');
$pdf->SetTitle(false);
$pdf->SetSubject(false);
$pdf->SetKeywords(false);
// set default header data.set all false because don't want to output header footer
$pdf->SetHeaderData(false, false, false, false);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(4, 11, 4);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
// define barcode style
$style = array(
'position' => '',
'align' => 'C',
'stretch' => false,
'fitwidth' => true,
'cellfitalign' => '',
'border' => true,
'hpadding' => 'auto',
'vpadding' => 'auto',
'fgcolor' => array(0, 0, 0),
'bgcolor' => false, //array(255,255,255),
'text' => true,
'font' => 'helvetica',
'fontsize' => 8,
'stretchtext' => 4
);
ob_start();
?>
<style type="text/css">
table {
width: 100%;
border-collapse: collapse;
}
td img {
height:10mm;
}
td {
padding: 0 1mm 0 1mm;
vertical-align:middle;
}
.cell {
width: 38mm;
height:21mm;
font-style: bold;
text-align: center;
}
tr {
height:21mm;
margin:0;
padding:0;
}
</style>
<?php
$i = 0;
$item = new item($db);
foreach ($_POST['checkbox'] as $id) {
$details = $item->getDetails($id);
$qt = (isset($_POST['qt'])) ? $_POST['qt'] : $details['qt'];
for ($cnt = 1; $cnt <= $qt; $cnt++) {
// check if it's the beginning of a new table
if ($i % 65 == 0)
echo '<table>';
// check if it's the beginning of a new row
if ($i % 5 == 0)
echo '<tr>';
echo '<td><div class="cell">www.fety.fr<br/>';
$pdf->Cell(0, 0, 'CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9', 0, 1);
$pdf->write1DBarcode($id, 'C39', '', '', '', 18, 0.4, $style, 'N');
$pdf->Ln();
echo '<br/>' . $details['hcode'] . '</div></td>';
// check if it's the end of a row
if (($i + 1) % 5 == 0)
echo '</tr>';
// check if it's the end of a table
if (($i + 1) % 65 == 0)
echo '</tr></table>';
$i++;
}
}
// if the last table isn't full, print the remaining cells
if ($i % 65 != 0) {
for ($j = $i % 65; $j < 65; $j++) {
if ($j % 65 == 0)
echo '<table>';
if ($j % 5 == 0)
echo '<tr>';
echo '<td></td>';
if (($j + 1) % 5 == 0)
echo '</tr>';
if (($j + 1) % 65 == 0)
echo '</table>';
}
}
$markup = ob_get_clean();
// output the HTML content
$pdf->writeHTML($markup, true, false, true, false, '');
// reset pointer to the last page
$pdf->lastPage();
// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('bcsheet.pdf', 'I');
?>
腳本就像是:
- 用戶選擇項複選框
- 表單提交後,PHP通過Ajax獲取複選框的值
- 在f oreach循環,PHP從數據庫中獲取每個項目的數量。
- 生成的表
準備給100代表解決此問題 – heron 2012-02-12 11:59:24
您是否考慮過使用原始PostScript?這是一項具有挑戰性的任務,但您可以完全控制渲染畫布。你也可以找到渲染條形碼的庫/教程。 – Tibo 2012-02-12 12:01:01
@Tibo我需要將結果輸出爲PDF或HTML格式。 HTML給出了不同的結果。所以我有1種方法 - PDF。所以需要讓它工作 – heron 2012-02-12 12:02:42