PHP将pdf文件转换成swf文件

用PHP开发皮肤科网站的时候,用到了在线浏览pdf文件的功能,js播放插件需要同时将pdf文件转换成swf文件两者兼用才能达到效果。


首先要在linux服务器上安装pdf转换为swf的转换工具

安装swftools
中文支持安装:
mkdir –p /usr/share/xpdf
cd /usr/share/xpdf/
下载中文支持及字体库
wget ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
wget http://www.nginxs.com/download/font.zip
tar zxvf xpdf-chinese-simplified.tar.gz
unzip font.zip
mv Gbsn00lp.ttf gkai00mp.ttf xpdf-chinese-simplified/CMap/ cd /usr/share/xpdf/xpdf-chinese-simplified
编辑,加入新增的字体

vim add-to-xpdfrc
内容如下:
cidToUnicodeAdobe-GB1/usr/share/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode
unicodeMapISO-2022-CN/usr/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap
unicodeMapEUC-CN/usr/share/xpdf/chinese-simplified/EUC-CN.unicodeMap
unicodeMapGBK/usr/share/xpdf/chinese-simplified/GBK.unicodeMap
cMapDirAdobe-GB1/usr/share/xpdf/chinese-simplified/CMap toUnicodeDir/usr/share/xpdf/chinese-simplified/CMap
displayCIDFontTT Adobe-GB1 /usr/share/xpdf/chinese-simplified/CMap/gkai00mp.ttf
保存后退出
SwfTool安装:
cd /usr/local/wenku
wget http://www.swftools.org/swftools-0.9.1.tar.gz
tar zxvf swftools-0.9.1.tar.gz
cd swftools-0.9.1
./configure 6)make
make install

测试一下是否可用

pdf2swf -o /usr/output.swf -T -z -t -f /usr/test1.pdf -s languagedir=/usr/local/share/xpdf/chinese-simplified -s flashversion=9

然后就是如何运用到项目中 如下

在项目中上传pdf文件到服务器的方法中

下面是执行文件转换的代码,$path1是转换的swf文件保存地址,$path是pdf文件路径

system("pdf2swf -o $path1 -T -z -t -f $path -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9 -s poly2bitmap",$out);

以上都完成就成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将PDF文件转换为Excel,可以考虑使用第三方库或在线转换工具。 1. 使用第三方库 可以使用 PHPExcel 库来处理 Excel 文件。首先需要将 PDF 文件转换为 CSV 文件,然后再使用 PHPExcel 将 CSV 文件转换为 Excel 文件。 以下是示例代码: ```php require_once 'PHPExcel/Classes/PHPExcel.php'; require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php'; // Convert PDF to CSV using pdftotext command $pdf_file = 'example.pdf'; $csv_file = 'example.csv'; shell_exec("pdftotext -layout $pdf_file $csv_file"); // Load CSV file into PHPExcel $objPHPExcel = PHPExcel_IOFactory::load($csv_file); // Save as Excel file $excel_file = 'example.xlsx'; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save($excel_file); ``` 2. 使用在线转换工具 可以使用在线转换工具如 Zamzar、PDFTables、SmallPDF 等将 PDF 文件转换为 Excel 文件。这些工具都提供了 API 或 SDK 可以与 PHP 集成使用。 以下是示例代码: ```php // Using Zamzar API $source_file = 'example.pdf'; $target_format = 'xlsx'; $zamzar_api_key = 'your_zamzar_api_key'; $zamzar_url = "https://api.zamzar.com/v1/jobs"; $data = array( "source_file" => curl_file_create($source_file), "target_format" => $target_format ); $headers = array( "Content-Type: multipart/form-data", "Accept: application/json", "Authorization: Basic " . base64_encode($zamzar_api_key . ":") ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $zamzar_url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); $job = json_decode($result); // Check status of the job $job_id = $job->id; $zamzar_api_key = 'your_zamzar_api_key'; $zamzar_url = "https://api.zamzar.com/v1/jobs/$job_id"; $headers = array( "Content-Type: application/json", "Accept: application/json", "Authorization: Basic " . base64_encode($zamzar_api_key . ":") ); while (true) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $zamzar_url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); $job = json_decode($result); if ($job->status == "successful") { // Download Excel file $excel_url = $job->target_files[0]->url; $excel_file = 'example.xlsx'; file_put_contents($excel_file, file_get_contents($excel_url)); break; } elseif ($job->status == "failed") { // Handle error break; } else { // Wait and check again sleep(5); } } ``` 注意:使用在线转换工具需要提供 API 密钥,并且可能需要付费。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值