search
HomeBackend DevelopmentPHP ProblemHow 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.

How to convert office files to pdf files in php

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools