【问题标题】:how can i read a hyperlink from a pdf using php ?如何使用 php 从 pdf 中读取超链接?
【发布时间】:2013-07-24 11:10:07
【问题描述】:

我有一个包含一些链接的 pdf。链接不会像 http://www.example.com/abcd.pdf。但是有一些文本链接到一些 url。我只想提取那个网址。

【问题讨论】:

  • 您是否能够从 PDF 文件中获取文本?如果没有,请查看:stackoverflow.com/questions/1882318/…。之后,您可以使用 REGEX(例如)在文本中搜索 URL。
  • 我尝试过使用其他 pdf 阅读器。我正在获取文本,但与文本关联的链接(url)没有获取。
  • 你想获取什么链接? preg_match_all 还是什么?发布您的代码..
  • @peter,我无法在此处发布所有代码。代码太长。我正在使用此代码。 webcheatsheet.com/php/reading_clean_text_from_pdf.php
  • 我是指从 PDF 中提取文本后的代码。

标签: php html pdf


【解决方案1】:

没有必要像我最初那样单独选择 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(如果有)。我的情况是这个 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);
    }

【讨论】:

    猜你喜欢
    • 2011-10-21
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2014-01-28
    相关资源
    最近更新 更多