mPDF 以 PDF 为中心进行打印

Posted

技术标签:

【中文标题】mPDF 以 PDF 为中心进行打印【英文标题】:mPDF center on PDF for print 【发布时间】:2016-01-04 13:29:30 【问题描述】:

我正在尝试将其显示为详细目录描述的中心。

我希望页面仅从 A1-G1 居中,然后我将详细产品目录合并并居中。问题是,当我输出到 PDF 时,它仍然在页面左侧,并且右侧有大量空白区域。

<?php
            include_once 'processor/dbconfig.php';
            include_once 'Classes/PHPExcel.php';

            /**
             * PHPExcel
             *
             * Copyright (C) 2006 - 2014 PHPExcel
             *
             * This library is free software; you can redistribute it and/or
             * modify it under the terms of the GNU Lesser General Public
             * License as published by the Free Software Foundation; either
             * version 2.1 of the License, or (at your option) any later version.
             *
             * This library is distributed in the hope that it will be useful,
             * but WITHOUT ANY WARRANTY; without even the implied warranty of
             * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
             * Lesser General Public License for more details.
             *
             * You should have received a copy of the GNU Lesser General Public
             * License along with this library; if not, write to the Free Software
             * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
             *
             * @category   PHPExcel
             * @package    PHPExcel
             * @copyright  Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
             * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
             * @version    1.8.0, 2014-03-02
             */

            /** Error reporting */


            /** Include PHPExcel */
            require_once 'Classes/PHPExcel/IOFactory.php';


            //  Change these values to select the Rendering library that you wish to use
            //      and its directory location on your server
            //$rendererName = PHPExcel_Settings::PDF_RENDERER_TCPDF;
            $rendererName = PHPExcel_Settings::PDF_RENDERER_MPDF;
            //$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
            //$rendererLibrary = 'tcPDF5.9';
            $rendererLibrary = '';
            //$rendererLibrary = 'domPDF0.6.0beta3';
            $rendererLibraryPath = 'Classes/mpdf/' . $rendererLibrary;

            $keyword = $_SESSION['btid'];
              $sql = "SELECT * FROM patients WHERE order_id = :keyword";
              $q=$con->prepare($sql);
              $q->bindvalue(":keyword", $keyword);
              $q->execute();
              $data=$q->fetch(PDO::FETCH_ASSOC);

              $pname = $data['first'] . " " . $data['last'];

              $street = $data['street'];

              $address = $data['city'] . "," . $data['state'] . " " . $data['zip'];

              $phone = $data['phone'];

              $dob = $data['dob'];








            // Create new PHPExcel object
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);
            $worksheet = $objPHPExcel->getActiveSheet();




            // Add some data
            $worksheet->mergeCells('A1:G1');
            $worksheet->mergeCells('C16:E16');
            $worksheet->mergeCells('C18:E18');
            $worksheet->mergeCells('C20:E20');
            $worksheet->mergeCells('C22:E22');
            $worksheet->mergeCells('C24:E24');
            $worksheet->mergeCells('C26:E26');
            $worksheet->mergeCells('C28:E28');
            //PATIENT SIDE

            $worksheet->SetCellValueByColumnAndRow(0, 1, 'DETAILED Catalog DESCRIPTION');
            $worksheet->SetCellValueByColumnAndRow(0, 3, 'Plan:');
            $worksheet->SetCellValueByColumnAndRow(0, 4, 'Order ID:');
            $worksheet->SetCellValueByColumnAndRow(0, 5, 'DOB:');
            $worksheet->SetCellValueByColumnAndRow(0, 6, 'ADDRESS:');
            $worksheet->SetCellValueByColumnAndRow(0, 8, 'PHONE:');
            $worksheet->SetCellValueByColumnAndRow(0, 10, 'LENGTH OF');
            $worksheet->SetCellValueByColumnAndRow(0, 11, 'NEED');
            $worksheet->SetCellValueByColumnAndRow(1, 3, $pname);
            $worksheet->SetCellValueByColumnAndRow(1, 6, $street);
            $worksheet->SetCellValueByColumnAndRow(1, 7, $address);
            $worksheet->SetCellValueByColumnAndRow(1, 5, $dob);
            $worksheet->SetCellValueByColumnAndRow(1, 8, $phone);
            $worksheet->SetCellValueByColumnAndRow(1, 11, '____________');
            $worksheet->SetCellValueByColumnAndRow(0, 16, 'Number');
            $worksheet->SetCellValueByColumnAndRow(1, 16, 'QTY');
            $worksheet->SetCellValueByColumnAndRow(2, 16, 'DESCRIPTION/MAKE/MODEL');

            //PROVIDER SIDE
            $worksheet->SetCellValueByColumnAndRow(4, 3, 'SUPPLIER NAME:');
            $worksheet->SetCellValueByColumnAndRow(4, 4, 'ADDRESS:');
            $worksheet->SetCellValueByColumnAndRow(4, 6, 'PHONE:');
            //WIDTH OF CELLS
            $worksheet->getStyle("A1:G27")->getFont()->setSize(10);
            $worksheet->getStyle("A1:G27")->getFont()->setName('Times New Roman');
            $worksheet->getColumnDimension('A')->setAutoSize(true);
            $worksheet->getColumnDimension('H')->setAutoSize(true);
            $worksheet->getColumnDimension('F')->setWidth(14);
            //CENTER ALIGN
            $worksheet->getStyle("A1:G1")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("C16:E16")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B18")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A18")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A16")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B16")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("F16")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("C20")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B20")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A20")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("C22")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B22")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A22")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("C24")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B24")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A24")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("C26")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B26")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A26")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("C28")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("B28")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $worksheet->getStyle("A28")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

            $worksheet->getStyle('A16:G17')->getFill()
                ->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
                ->setStartColor(new PHPExcel_Style_Color(PHPExcel_Style_Color::COLOR_YELLOW))
                ->setRotation(45);

            $objPHPExcel->getActiveSheet()->setShowGridLines(false);

            if (!PHPExcel_Settings::setPdfRenderer(
                    $rendererName,
                    $rendererLibraryPath
                )) 
                die(
                    'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
                    '<br />' .
                    'at the top of this script as appropriate for your directory structure'
                );
            


            // Redirect output to a client’s web browser (PDF)
            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment;filename="new.pdf"');

            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
            $objWriter->save('php://output');
            exit;

【问题讨论】:

【参考方案1】:

一个老问题:但以防万一有人还在研究同样的问题。 几年前也遇到过 mPDF 的居中问题。不仅如此,还有很多问题。

mPDF 不支持许多 CSS 样式:一些是浮动和文本对齐,仅举几例。此外,PHPExcel 正在 PHPExcel_Style_Alignment 上使用 text-align,如下面的函数所示。

private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle) 
    $css = array();
    $css['vertical-align'] = $this->_mapVAlign($pStyle->getVertical());
    if ($textAlign = $this->_mapHAlign($pStyle->getHorizontal())) 
        $css['text-align'] = $textAlign;
    
    return $css;
 

解决方法是在 td 标记内使用这个(在普通表上尝试):

<th align="left"></th>

只是不确定如何在 PHPExcel 上添加它。

【讨论】:

以上是关于mPDF 以 PDF 为中心进行打印的主要内容,如果未能解决你的问题,请参考以下文章

mPDF 自动打印问题

Graphics.DrawString 以打印文档宽度为中心

使用 mpdf 创建的可填写 PDF 未显示复选框

使用 MPDF 将 HTML 保存为 PDF 和电子邮件

mPDF 文档自动高度(POS 打印机)

为啥 mPDF 生成的 PDF 文件总是保存为“mpdf.pdf”