php word转pdf 读取pdf内容
1.word转pdf 需要用到 LibreOffice 便携版 如果是linux可以去官网查看安装命令
下载地址:https://zh-cn.libreoffice.org/download/portable-versions/
$retval = 1; // exec() might be disabled url需要填写相对地址 $cmd = "D:\LibreOfficePortable/LibreOfficeWriterPortable.exe --headless --convert-to pdf D:\phpstudy_pro\WWW\ReadWord\upload\\$name.docx --outdir D:\phpstudy_pro\WWW\ReadWord\upload\\"; if (function_exists('exec')){ @exec($cmd, $output, $retval); } // Did it work? if ($retval > 0){ exit('process_failed'); }else{ sleep(5); header('location:./phpword.php?name='.$name); }
2.pdf 读取
1.需要安装composer
2.在项目下面 composer require smalot/pdfparser 安装扩展 安装成功你会看到 vendor文件夹
include 'vendor/autoload.php'; $parser = new \Smalot\PdfParser\Parser(); $pdf = $parser->parseFile("./upload/$name.pdf"); $text = $pdf->getText(); //读取出来的内容是字符串 // Retrieve all pages from the pdf file. $pages = $pdf->getPages(); // Loop over each page to extract text. foreach ($pages as $page) { echo $text = $page->getText(); // $text = explode("\n",$text); // echo '<pre>'; // print_r($text); // foreach($text as $kk => $vv){ // echo $vv.'<br>'; // } }