1

I am using PHPExcel to create an excel file! I need to save it as .xlsx file and to have a .pdf file

With PHPExcel my pdf appears in a strange format, like this: Result

But I want something like this (this was manually generated, "save as pdf"): What I Want

Do you know a simple way to convert the excel to pdf?

$objReader = \PHPExcel_IOFactory::createReader("Excel2007");
$objPHPExcel = $objReader->load(Some_Path);
$objPHPExcel->setActiveSheetIndex(0);        


$objPHPExcel->getActiveSheet()
        ->setCellValue('B8', "testing");


//Write Excel 
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('testing.xlsx');

// Write PDF
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('testing.pdf');
7
  • can we have some code plz :)
    – MaximeK
    Sep 5, 2016 at 15:48
  • Yes! I'm sorry @MaximeK :)
    – Erbi Silva
    Sep 5, 2016 at 15:55
  • Which PDF Rendering engine are you using? What settings have you enabled for gridlines? setShowGridlines() should be set to false for the worksheet
    – Mark Baker
    Sep 5, 2016 at 15:56
  • You probably also need to do some cell merging, or set some column widths
    – Mark Baker
    Sep 5, 2016 at 16:13
  • Are you using PC? If so, PHP can make a COM interface to the Excel object library and use its native ExportAsFixedFormat method
    – Parfait
    Sep 5, 2016 at 18:17

1 Answer 1

2

Consider a COM interface to the Excel object library if using PHP for Windows PC. This is a Windows-only extension and usually ships with PHP installation on PCs.

This approach allows you to do practically anything Excel VBA can do including calling the ExportAsFixedFormat method to output PDF files. Do note this method can be run on Workbook or Worksheet objects (adhering to preset/default print page settings), even Chart and Range.

// EXCEL APP OBJECT
$xlapp =  new COM("Excel.Application") or Die ("Did not instantiate Excel");

// WORKBOOK AND WORKSHEET OBJECTS
$wbk = $xlapp->Workbooks->Open("C:\\Path\\To\\Workbook.xlsx");    
$wks = $wbk->Worksheets(1);

// SET CELL VALUE
$wks->Range("B8")->Value = "testing";

// OUTPUT WORKSHEET TO PDF
$xlTypePDF = 0;
$xlQualityStandard = 0;

try {
    $wks->ExportAsFixedFormat($xlTypePDF, "C:\\Path\\To\\Output.pdf", $xlQualityStandard);

} catch(com_exception $e) {  
    echo $e->getMessage()."\n";
    exit;

}

// OPEN WORKBOOK TO SCREEN
$xlapp->Visible = true;

// END PROCESS / FREE RESOURCES
$xlapp = NULL;
unset($xlapp);
3
  • Can I somehow simulate this COM interface on Linux server?
    – user4965214
    May 2, 2017 at 7:59
  • Unfortunately, COM is a Windows technology. You may have to write a macro in an Excel workbook that does saw thing and have PHP call it via exec.
    – Parfait
    May 2, 2017 at 11:22
  • is there any way to change the pdf setting for exmple i havetwo graphs in excel when i convert to pdf both grahs come in one line and beacuse of space second graph divides into two pages
    – manjinder
    Dec 16, 2019 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.