gpt4 book ai didi

php - 在 PHP 中处理 PDF

转载 作者:可可西里 更新时间:2023-11-01 12:36:32 26 4
gpt4 key购买 nike

我想做的是,准备 PHP 生成的结果以使用严格的规则进行打印。

用 css+html 尝试了所有可能的方法:以 px、mm、cm 为单位设置尺寸。没有任何帮助。每个浏览器,甚至每个打印机都打印出完全不同的纸张结果(尝试使用和不使用边框打印。也没有得到结果)。经过长时间的研究,发现 CSS 并不是实现此目的的最佳方式,而是更好的方式——使用 PHP 的 pdf 创建功能。所以,安装了 TCPDF。但是无法让它与我为 HTML 输出创建的逻辑部分一起工作。

我想得到什么

  • 表格的第一行和最后一行距纸张顶边和底边的边距必须为11 毫米
  • 行间距' 0 mm
  • 表格的行必须距离纸张的左右4 毫米
  • 2 毫米 每列之间
  • 38 毫米宽 x 21.2 毫米高每个单元格
  • 13 x 行,5 x 列,13x5=65 个单元格
  • 新页面中的每个表。换句话说 - 在每个表格分页符
  • 之后
  • 每个单元格中的Code 39条码(值必须是$id)
  • 只有 PDF 结果中的表格 - 没有页眉,没有页脚,没有标题......等等

这里有更详细的图片说明:

enter image description here

我得到了什么

表单提交后,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');
?>

脚本是这样工作的:

  1. 用户选择项目的复选框
  2. 表单提交后,PHP通过Ajax获取选中复选框的值
  3. 在 foreach 循环中,PHP 从数据库中获取每个项目的数量。
  4. 生成表格

最佳答案

这是一个 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;

?>

关于php - 在 PHP 中处理 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com