<?php
function dieString(){die("<h1>Forbidden</h1>".date("Y-m-d H:i:s"));}
date_default_timezone_set('Asia/Shanghai');//'Asia/Shanghai' 亚洲/上海
if(empty($_REQUEST['word_base64'])){dieString();}
$file_content=base64_decode($_REQUEST['word_base64']);
$starttime = explode(' ',microtime());
$_microtime = round(($starttime[0]+$starttime[1]),6);
$tmpFileName = date('YmdHis.').$_microtime.'.'.uniqid().'.docx';
$oldFile = __DIR__.DIRECTORY_SEPARATOR.'tmp'.DIRECTORY_SEPARATOR.$tmpFileName;
$newFile = $oldFile.'.pdf';
file_put_contents($oldFile,$file_content);
include('./includes/DocConverter.php');
$doc = new DocConverter();
$doc->DoctPdf($oldFile,$newFile);
$result = base64_encode(file_get_contents($newFile));
@unlink($oldFile);
@unlink($newFile);
die($result);
DocConverter.php
<?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();
$word = null;
// echo 'topdf suceess:' . $destfilename;
} catch (\Exception $e) {
if (method_exists($word, 'Quit')){
$word->Quit();
}
$word = null;
//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;
}
}
}
test.cli.php
<?php
$oldFile = __DIR__.'/test.docx';
$newFile = __DIR__.'/test.docx.pdf';
include('./includes/DocConverter.php');
$doc = new DocConverter();
$doc->DoctPdf($oldFile,$newFile);
test.web.php
<?php
// http://212.64.92.190/testpdf.php
$data = ['word_base64' => base64_encode(file_get_contents(__DIR__ . '/test.docx'))];
$url = 'http://localhost/getpdf.php';
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => http_build_query($data) ,
'timeout' => 20
)
));
$pdf_base64 = file_get_contents($url, false, $context);
// die($pdf_base64);
file_put_contents(__DIR__ . '/test.pdf', base64_decode($pdf_base64));
die('测试完成');
系统接口被频繁的请求,