我如何使用PHP从pdf中读取超链接?

时间:2013-07-24 11:10:07

标签: php html pdf

我有一个包含一些链接的pdf。链接不像http://www.example.com/abcd.pdf。但有些文字与某些网址相关联。我只是想提取那个网址。

1 个答案:

答案 0 :(得分:0)

没有必要像我最初那样单独选择pdf阅读选项。我们可以通过fopen()方法或file_get_contents()方法简单地读取pdf文件。

    $pdf_content = file_get_contents($actual_pdf_file, true);
    preg_match_all('/URI\(([^,]*?)\)\/S\/URI/', $pdf_content, $matches);

我根据我的要求编写了这个preg_match_all函数。每个链接都有URI。

现在我们将获取$ matches数组中的url。我的情况这个网址是一个pdf下载链接。从链接下载pdf的代码如下......

foreach($matches[1] as $pdfurl)
    {       
    $CurlConnect = curl_init();
    curl_setopt($CurlConnect, CURLOPT_URL, $pdfurl);
    curl_setopt($CurlConnect, CURLOPT_POST, 1);
    curl_setopt($CurlConnect, CURLOPT_RETURNTRANSFER, 1);
    @curl_setopt($CurlConnect, CURLOPT_POSTFIELDS, $request);
    $Result = curl_exec($CurlConnect);
    $new_down_pdf='new_pdf_name.pdf';
    file_put_contents($new_down_pdf,$Result);
    }