Converting excel to pdf using PHP

后端 未结 1 1715
一生所求
一生所求 2020-12-20 08:11

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: Res

相关标签:
1条回答
  • 2020-12-20 08:39

    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);
    
    0 讨论(0)
提交回复
热议问题