Home > Article > Backend Development > How to convert office files to pdf files in php
How to convert php office to pdf: first open the "php.ini" file; then enable the dcom extension; then configure the office component service; and finally run the code file to convert the office file into pdf.
Recommendation: "PHP Video Tutorial"
The methods to convert office files into pdf are
1. Use the services provided by openoffice (relatively simple, but the conversion effect is not very good)
2. Use the services provided by office (note: this is on a windows server, and A higher version of office is installed on the server)
The following focuses on using the office service to convert office files into pdf
1.php opens the dcom extension
Open php.ini , search for php_com_dotnet and php_com_dotnet:
extension=php_com_dotnet.dll //把前面的分号去掉 com.allow_dcom = true //改为true
Restart apache
2. Configure office component service
.
There are two more operations like this! !
3. Now it’s time to introduce the code to convert office files into pdf
(1)ppt to pdf code
1 public function ppt_to_pdf() { 2 $srcfilename = 'E:/aa.ppt'; 3 $destfilename = 'E:/aa.pdf'; 4 try { 5 if(!file_exists($srcfilename)){ 6 return; 7 } 8 $ppt = new \COM("powerpoint.application") or die("Unable to instantiate Powerpoint"); 9 $presentation = $ppt->Presentations->Open($srcfilename, false, false, false); 10 $presentation->SaveAs($destfilename,32,1); 11 $presentation->Close(); 12 $ppt->Quit(); 13 } catch (\Exception $e) { 14 if (method_exists($ppt, "Quit")){ 15 $ppt->Quit(); 16 } 17 return; 18 } 19 }
(2)excel to pdf code
1 public function excel_to_pdf() { 2 $srcfilename = 'E:/aa.xls'; 3 $destfilename = 'E:/aa.pdf'; 4 try { 5 if(!file_exists($srcfilename)){ 6 return; 7 } 8 $excel = new \COM("excel.application") or die("Unable to instantiate excel"); 9 $workbook = $excel->Workbooks->Open($srcfilename, null, false, null, "1", "1", true); 10 $workbook->ExportAsFixedFormat(0, $destfilename); 11 $workbook->Close(); 12 $excel->Quit(); 13 } catch (\Exception $e) { 14 echo ("src:$srcfilename catch exception:" . $e->__toString()); 15 if (method_exists($excel, "Quit")){ 16 $excel->Quit(); 17 } 18 return; 19 } 20 }
(3)word to pdf code (other text format files can also use this, for example: txt file)
1 public function doc_to_pdf() { 2 $srcfilename = 'E:/aa.doc'; 3 $destfilename = 'E:/aa.pdf'; 4 try { 5 if(!file_exists($srcfilename)){ 6 return; 7 } 8 9 $word = new \COM("word.application") or die("Can't start Word!"); 10 $word->Visible=0; 11 $word->Documents->Open($srcfilename, false, false, false, "1", "1", true); 12 13 $word->ActiveDocument->final = false; 14 $word->ActiveDocument->Saved = true; 15 $word->ActiveDocument->ExportAsFixedFormat( 16 $destfilename, 17 17, // wdExportFormatPDF 18 false, // open file after export 19 0, // wdExportOptimizeForPrint 20 3, // wdExportFromTo 21 1, // begin page 22 5000, // end page 23 7, // wdExportDocumentWithMarkup 24 true, // IncludeDocProps 25 true, // KeepIRM 26 1 // WdExportCreateBookmarks 27 ); 28 $word->ActiveDocument->Close(); 29 $word->Quit(); 30 } catch (\Exception $e) { 31 if (method_exists($word, "Quit")){ 32 $word->Quit(); 33 } 34 return; 35 } 36 }
The above is the detailed content of How to convert office files to pdf files in php. For more information, please follow other related articles on the PHP Chinese website!