her*_*ron 8 php printing pdf tcpdf
我想要做的是,准备PHP生成的结果用于强大规则的打印.
用css + html尝试了所有可能的方法:用px,mm,cm设置尺寸.什么都没有帮助.每个浏览器,甚至每个打印机都打印出完全不同的纸张结果(尝试使用&w/o边框打印.也没有得到结果).经过长时间的研究,发现CSS不是用于此目的的最佳方式和更好的方式 - 使用PHP的pdf创建功能.所以,安装了TCPDF.但是无法使用我为HTML输出创建的逻辑部分.
$id)以下是对图像的更详细说明:

表单提交后,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');
?>
Run Code Online (Sandbox Code Playgroud)
这是一个 html/css 到 pdf 转换器库http://www.mpdf1.com/mpdf/
它有自己的 html/css 解析器,因此在所有浏览器中都会产生相同的结果。
<?php
$html = '
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
tr {
}
td {
width: 38mm;
height: 21.2mm;
margin: 0 1mm;
text-align: center;
vertical-align:middle;
}
</style>
</head>
<body>
<table>';
for ($i = 0; $i < 13; $i++)
{
$html .= '<tr>';
for($j = 0; $j < 5; $j++)
{
$html .= '<td><barcode code="TEC-IT" type="C39" class="barcode" /></td>';
}
$html .= '</tr>';
}
$html .= '</table>
</body>
</html>';
include("MPDF53/mpdf.php");
$mpdf = new mPDF('c', 'A4', '', '', 4, 4, 10.7, 10.7, 0, 0);
$mpdf->SetDisplayMode('fullpage');
$mpdf->list_indent_first_level = 0;
$mpdf->WriteHTML($html,0);
$mpdf->Output('test.pdf','I');
exit;
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13106 次 |
| 最近记录: |