在PHP中使用PDF

her*_*ron 8 php printing pdf tcpdf

我想要做的是,准备PHP生成的结果用于强大规则的打印.

用css + html尝试了所有可能的方法:用px,mm,cm设置尺寸.什么都没有帮助.每个浏览器,甚至每个打印机都打印出完全不同的纸张结果(尝试使用&w/o边框打印.也没有得到结果).经过长时间的研究,发现CSS不是用于此目的的最佳方式和更好的方式 - 使用PHP的pdf创建功能.所以,安装了TCPDF.但是无法使用我为HTML输出创建的逻辑部分.

我想得到什么

  • 表格纸张顶部和底部的第一行和最后一行的边距必须为11毫米
  • 行之间的边距为0 mm
  • 表格的行必须距离纸张的左右两侧4毫米
  • 每列之间2毫米
  • 每个单元38毫米宽x 21.2毫米高
  • 13行,5 x列,13x5 = 65个单元格
  • 新页面中的每个表格.换句话说 - 每个表分页后
  • 在每个单元格Code 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');
?>
Run Code Online (Sandbox Code Playgroud)

脚本的作用是这样的:

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

red*_*777 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;

?>
Run Code Online (Sandbox Code Playgroud)