如何在不下载的情况下将带有 pdf 扩展名的 url 的内容转换为文本以便在 PHP 中解析


How can the contents of a url with a pdf extension be converted to text for parsing in PHP without downloading?

如何在不下载的情况下将带有pdf扩展名的URL的内容转换为文本以便在PHP中进行解析?

我看到如何做到这一点(没有垃圾字符)的唯一方法是将文件下载到服务器文件夹并外壳将二进制转换为文本的可执行文件。

以下是我找到的一些可执行库:TET,文本提取工具包xpdf

我宁愿在不先下载 pdf 的情况下转换 URL pdf(例如打开二进制文件然后转换)。

有没有办法在不下载 PHP 中的 pdf 的情况下做到这一点?

建议使用哪种方法实现最快的执行时间?

作为快速说明,我将使用 pdf 扩展名处理大约 64 个 URL,并非所有这些 URL 实际上都指向 pdf。 事实上,其中一些网址可能指向别名 html 页面,而不一定指向 pdf 文件,因此在使用转换工具之前需要辨别差异。

直接从

URL转换是不切实际的,而且会very slow.. 大多数转换是通过command line完成的,而不是直接使用PHP完成的,以获得快速更好的结果

使用xpdf pdftotext进行样本转换

安装 (Linux) apt-get install xpdf

示例代码

$file = $directory . '/' . $filename;
$fileinfo = pathinfo ( $filename );
$content = "";
// pdt to text
if ($fileinfo ['extension'] == 'pdf') {
    $outpath = preg_replace ( "/'.pdf$/", "", $file ) . ".txt";
    system ( "pdftotext -enc UTF-8 " . escapeshellcmd ( $file ), $ret );
    if ($ret == 0) {
        $content = file_get_contents ( $outpath );
        unlink ( $outpath );
    }
}

我找到了一些源代码,它们采用了file_get_contents('url.pdf')的内容并进行了粗略的转换(我的意思是非常粗糙的)。

由于这似乎是在内存中进行此转换的最佳方法,因此我想我别无选择,只能先下载" url.pdf"。

这段代码可以下载文件吗?

//set to the URL of the file you want to download:
$inPath = "http://somepage.com/hello.jpg";
//set to the local path where the file should be saved:
$outPath = "/usr/local/htdocs/hello.jpg";
$in = fopen($inPath, "rb");
$out = fopen($outPath, "wb");
while ($chunk = fread($in,8192) ) {
fwrite($out, $chunk, 8192);
}
fclose($in);
fclose($out);