在PHP中使用PDF

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了在PHP中使用PDF脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

我想得到什么

>表格从纸张顶部和底部开始的第一行和最后一行的边距必须为11毫米
>行之间的边距为0 mm
>表格的行必须距离纸张的左右两侧4毫米
每列之间> 2 mm
> 38 mmx每个单元高21.2 mm
> 13行,5 x列,13×5 = 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);

// 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),'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,'CODE 39 - ANSI Mh10.8M-1983 - USD-3 - 3 of 9',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,'');



// reset pointer to the last page
$pdf->lastPage();

// ---------------------------------------------------------
//Close and output PDF document
$pdf->Output('bcsheet.pdf','I');
?>

脚本的作用是这样的:

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

这是一个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,10.7,0);

    $mpdf->SetDisplayMode('fullpage');

    $mpdf->list_indent_First_level = 0;

    $mpdf->WriteHTML($html,0);

    $mpdf->Output('test.pdf','I');
    exit;

?>

脚本宝典总结

以上是脚本宝典为你收集整理的在PHP中使用PDF全部内容,希望文章能够帮你解决在PHP中使用PDF所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。