px-core / libre-office-converter
将文件从一种格式(如 '.docx' 或 '.xlsx')转换为另一种格式(如 '.pdf' 或 '.csv')
dev-master
2016-04-07 11:36 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-09-27 04:30:02 UTC
README
Symfony 扩展,可以将文件从一种格式(如 '.docx' 或 '.xlsx')转换为另一种格式(如 '.pdf' 或 '.csv')。
支持的格式:.docx .doc .xlsx .xls .ppt .pdf .csv .odt .odp .ods 以及更多
作者
- Safwen Toukabri safwen.toukabri@proxym-it.com
需求
安装
使用 composer 下载扩展包
$ composer require px-core/libre-office-converter "dev-master"
Composer 会将扩展包安装到你的项目 vendor/px-core/libre-office-converter 目录下。
在内核中启用扩展包
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new pxCore\LibreOfficeConverterBundle\pxCoreLibreOfficeConverterBundle()
// ...
);
}
如果尚未安装 libreoffice,请使用以下命令
$ sudo apt-get install libreoffice
配置 libreoffice 参数(libreoffice 库的路径)
parameters:
# ...
libreoffice: /usr/bin/libreoffice # Default value
在 Unix 或 Linux 变体上,LibreOffice 可执行文件通常位于 /usr/lib/libreoffice/program/soffice
(链接:/usr/bin/libreoffice)
使用方法
示例1:将 WORD 转换为 PDF
// Create a new instance of pxCore_libreOffice_converter_service
$wordToPdfService = $this->get('pxCore_libreOffice_converter_service');
$webDir = __DIR__ . '/../../../../web';
// The word file path
$wordPath = $webDir . '/word/test.docx';
// The outDir in wich we will put the generated PDF
$outDir = $webDir . '/pdf';
$toFormat = 'pdf';
// Generate the PDF file
$wordToPdfService->convert($wordPath, $outDir, $toFormat);
示例2:将 XLSX 转换为 CSV
// Create a new instance of pxCore_libreOffice_converter_service
$pxCoreLibreOfficeConverter = $this->get('pxCore_libreOffice_converter_service');
$webDir = __DIR__ . '/../../../../web';
// The xlsx file path
$xlsxPath = $webDir . '/xlsx/test2.xlsx';
// The outDir in wich we will put the generated csv file
$outdir = $webDir . '/csv';
$toFormat = 'csv';
// Generate the csv file
$pxCoreLibreOfficeConverter->convert($xlsxPath, $outdir, $toFormat);
享受吧!