短
我想要做的是准备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