-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDocConverter.php
103 lines (100 loc) · 4.09 KB
/
DocConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* 转换文件类
* Class DocConverter
* $srcfilename 源文件名(绝对路径)
* $destfilename 目标文件名(绝对路径)
*/
class DocConverter {
//文本文件转PDF,.doc、.docx、.txt等
public function DoctPdf($srcfilename,$destfilename = '') {
if($destfilename == '') $destfilename = __DIR__ . '\DocConverter.pdf';
$srcfilename = str_replace('/', DIRECTORY_SEPARATOR , $srcfilename);
$converttype = 0;
try {
if(!file_exists($srcfilename)){
echo $srcfilename . ' is not exists';
return;
}
$word = new \COM('word.application') or die("Can't start Word!");
$word->Visible=0;
$word->Documents->Open($srcfilename, false, false, false, '1', '1', true);
$word->ActiveDocument->final = false;
$word->ActiveDocument->Saved = true;
$converttypetag;
if ($converttype == 1) {
$converttypetag = 2; // wdExportCreateWordBookmarks
} else {
$converttypetag = 1; // wdExportCreateHeadingBookmarks;
}
$word->ActiveDocument->ExportAsFixedFormat(
$destfilename,
17, // wdExportFormatPDF
false, // open file after export
0, // wdExportOptimizeForPrint
3, // wdExportFromTo
1, // begin page
5000, // end page
7, // wdExportDocumentWithMarkup
true, // IncludeDocProps
true, // KeepIRM
$converttypetag // WdExportCreateBookmarks
);
$word->ActiveDocument->Close();
$word->Quit();
echo 'topdf suceess:' . $destfilename;
} catch (\Exception $e) {
if (method_exists($word, 'Quit')){
$word->Quit();
}
echo '[convert error]:' . $e->__toString();
return;
}
}
//Excel转PDF
public function ExceltPdf($srcfilename,$destfilename = '') {
if($destfilename == '') $destfilename = __DIR__ . '\EXcelConverter.pdf';
$srcfilename = str_replace('/', DIRECTORY_SEPARATOR , $srcfilename);
try {
if(!file_exists($srcfilename)){
echo $srcfilename . ' is not exists';
return;
}
$excel = new \COM('excel.application') or die('Unable to instantiate excel');
$workbook = $excel->Workbooks->Open($srcfilename, null, false, null, '1', '1', true);
$workbook->ExportAsFixedFormat(0, $destfilename);
$workbook->Close();
$excel->Quit();
echo 'topdf suceess:' . $destfilename;
} catch (\Exception $e) {
if (method_exists($excel, 'Quit')){
$excel->Quit();
}
echo '[convert error]:' . $e->__toString();
return;
}
}
//PPT转PDF
public function PPTtPdf($srcfilename,$destfilename = '') {
if($destfilename == '') $destfilename = __DIR__ . '\PPTConverter.pdf';
$srcfilename = str_replace('/', DIRECTORY_SEPARATOR , $srcfilename);
try {
if(!file_exists($srcfilename)){
echo $srcfilename . ' is not exists';
return;
}
$ppt = new \COM('powerpoint.application') or die('Unable to instantiate Powerpoint');
$presentation = $ppt->Presentations->Open($srcfilename, false, false, false);
$presentation->SaveAs($destfilename,32,1);
$presentation->Close();
$ppt->Quit();
echo 'topdf suceess:' . $destfilename;
} catch (\Exception $e) {
if (method_exists($ppt, 'Quit')){
$ppt->Quit();
}
echo '[convert error]:' . $e->__toString();
return;
}
}
}