Converting excel to pdf using PHP

0 votes

I'm making an excel file with PHPExcel! I must create an a.pdf file and save it as an.xlsx file. My PDF shows in an odd format while using PHPExcel.

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 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');
Oct 23 in Others by Kithuzzz
• 20,660 points
213 views

1 answer to this question.

0 votes

If utilizing PHP on a Windows PC, take into account a COM interface  to the Excel object library. This Windows-only add-on typically comes with PHP installation on computers.

Practically anything that Excel VBA can do may be done with this method, including utilizing the ExportAsFixedFormat method to generate PDF files. As long as the print page settings are adhered to, this method can be used on Workbook or Worksheet objects, 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);
answered Oct 23 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

Java Spring - Writing Excel file and converting to PDF

Since you are using Spring I suggest ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
244 views
0 votes
1 answer

Converting all tabs of excel sheet to PDF

Using VBA, try it like this, for ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
205 views
0 votes
1 answer

Convert Excel to PDF using JavaScript

You're clobbering objExcel on line 15: var objExcel ...READ MORE

answered Oct 9 in Others by narikkadan
• 37,660 points
328 views
0 votes
1 answer

Using Java to create PDF graphs based on Excel formulas

The Apache POI project, an API for ...READ MORE

answered Oct 22 in Others by narikkadan
• 37,660 points
73 views
0 votes
1 answer

How to upload excel file to php server from <input type="file">

You first need to upload the file ...READ MORE

answered Jun 9 in JQuery by gaurav
• 22,040 points
1,887 views
0 votes
1 answer

Codeigniter convert excel file to pdf

This is a basic php script for ...READ MORE

answered Sep 27 in Others by narikkadan
• 37,660 points
176 views
0 votes
1 answer

How to create Dropdown list in excel using php

Try this: $objValidation = $objPHPExcel->getActiveSheet()->getCell('B'.$i)->getDataValidation(); $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST ); $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION ...READ MORE

answered Oct 3 in Others by narikkadan
• 37,660 points
137 views
0 votes
1 answer

How can I convert excel file to pdf using TCPDF?

PHPExcel can only read charts from Excel2007 ...READ MORE

answered Oct 21 in Others by narikkadan
• 37,660 points
130 views
0 votes
1 answer

How to print an Excel Sheet using VBA with Nitro PDF Creator in Excel 2016

you can use the built-in excel facilities ...READ MORE

answered Sep 24 in Others by narikkadan
• 37,660 points
119 views
0 votes
1 answer

Convert Excel and Word files to PDF Using ruby

 You can combine some: For excel files - ...READ MORE

answered Sep 26 in Others by narikkadan
• 37,660 points
135 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP