0

I am using Mpdf to generate a pdf from a phpSpreadsheet object. It works pretty well, but if a numeric cell is followed by a string cell I get no gap between the cells. I suspect this is because the numeric cell is right-justified by default while the string is left-justified by default.

Example of poorly formatted output

If I generate an xlsx file there is no problem. Is there a way I can add padding so that the pdf output looks better?

<?php require_once __DIR__ . "/vendor/autoload.php"; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\IOFactory; $spreadsheet = new Spreadsheet(); $spreadsheet->getActiveSheet() ->getColumnDimension('A') ->setWidth(12); $spreadsheet->getActiveSheet() ->getColumnDimension('B') ->setWidth(12); $spreadsheet->getActiveSheet() ->getColumnDimension('C') ->setWidth(12); $spreadsheet->getActiveSheet() ->setCellValue("A1", 1234567) ->setCellValue("B1", 'ABCDEFG') ->setCellValue("C1", 'QWERTY'); header('Content-Type: application/pdf'); header('Content-Disposition: attachment;filename="test.pdf"'); header('Cache-Control: max-age=0'); $objwriter = IOFactory::createwriter($spreadsheet,'Mpdf');; $objwriter->save("php://output"); 
0

1 Answer 1

0

<?php

require_once _DIR_ . "/vendor/autoload.php";

use PhpOffice\PhpSpreadsheet\Spreadsheet;

use PhpOffice\PhpSpreadsheet\IOFactory;

use PhpOffice\PhpSpreadsheet\Style\Alignment; // Import the Alignment class

$spreadsheet = new Spreadsheet();

$sheet = $spreadsheet->getActiveSheet();

$sheet->getColumnDimension('A')->setWidth(12);

$sheet->getColumnDimension('B')->setWidth(12);

$sheet->getColumnDimension('C')->setWidth(12);

// Set value and alignment for numeric cell

$sheet->setCellValue("A1", 1234567);

$sheet->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT); // Force left alignment

// Set value for string cells (default is usually left-aligned)

$sheet->setCellValue("B1", 'ABCDEFG');

$sheet->setCellValue("C1", 'QWERTY');

header('Content-Type: application/pdf');

header('Content-Disposition: attachment;filename="test.pdf"');

header('Cache-Control: max-age=0');

$objwriter = IOFactory::createWriter($spreadsheet, 'Mpdf');

$objwriter->save("php://output");

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I don't want my numeric cells to left aligned.
The answer should not be only code. Provide a clear explanation. Code belongs in a code block.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.