6

I am trying to center where it says Detailed Catalog Description.

I want the page to to be centered only from A1-G1 and then I merged and centered Detailed Product Catalog in that. The Problem is that, when I output to PDF it still way off to the left on the page and there is tons of blank space on the right.

<?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;
Snazzy Sanoj
  • 804
  • 1
  • 11
  • 28
Kmiles1990123
  • 189
  • 2
  • 12

1 Answers1

0

An old question: but just in case someone was still looking at this same issue.

Encountered centering issue with mPDF a few years ago as well.
Not just this but a lot of issues as well.

There are a lot of CSS styles not supported by mPDF: some are floats and text-aligns just to name a few. Also, PHPExcel is using text-align on PHPExcel_Style_Alignment as shown in the function below.

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;
} 

A workaround for this was using this inside td tag (tried on a normal table):

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

Just not sure how to add this on PHPExcel.

Severino Lorilla Jr.
  • 1,637
  • 4
  • 20
  • 33