<?php
namespace common;
use common\PDF;
/**
* 提供Office管理服务
* @name Office.php
* @author mengchen
* @version 1.0
* @copyright CHANGE INC
* @since 2023-02-15
*/
class Office
{
/**
* 定义根目录
*/
public $rootPath;
public function __construct()
{
if (!defined('ROOT_PATH')) {
$this->rootPath = (new \think\App())->getRootPath();
} else {
$this->rootPath = ROOT_PATH;
}
}
/**
* office转pdf
* @param string $url
*/
public function officeToPdf($url = '')
{
$fileName = getFileName(basename($url));
// 设置具体存放路径
$basePath = sprintf('%spublic/upload/files/%s', $this->rootPath, date('Ymd'));
$path = sprintf('%s/%s.pdf', $basePath, $fileName);
if (!file_exists($basePath)) {
mkdir($basePath, 0755, true); //创建目录
}
if (file_exists($path)) {
$pdf = new PDF();
$pngs = $pdf->pdfToPng($path);
if (count($pngs) > 0) {
unlink($path);
return $pngs;
}
return [];
}
//创建Command命令
$command = sprintf('libreoffice --headless --convert-to pdf %s --outdir %s', $url, $basePath);
//执行Command命令
$output = exec($command);
//判断执行结果
if (strpos($output, 'convert') !== false && file_exists($path)) {
$pdf = new PDF();
$pngs = $pdf->pdfToPng($path);
if (count($pngs) > 0) {
unlink($path);
return $pngs;
}
return [];
} else {
return [];
}
}
}