Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract all links from pdf file?

Tags:

python

pdf

pypdf

By standard, links are hiding in Annotations (section 12.5.6.5 from specifications). It is easy to extract address from there: Extracting links to pages in another PDF from PDF using Python or other method But very often links are presented not like special objects in document, but as plain text like "http://blah-blah.com". How do I extract not only links from annotations, but links from text itself? I can search through the whole text and finding words like "http://", but is there more optimal solution? PDF editors are highlighting text-links too, how do they know that this piece of text is hyperlink?

like image 312
m9_psy Avatar asked Jul 15 '15 16:07

m9_psy


People also ask

How do I find an embedded link in a PDF?

Searching For a Link In the Find dialog box, enter the search string and tick Hyperlinks. Then click Find All. A window will open showing all of the matches for your search string, together with the page number where the match appears.


2 Answers

I've just made pdfx, a small tool for exactly this job: to download all PDFs from a given PDF. It's written in Python and released as open source under the GPLv3 license: http://www.metachris.com/pdfx

You can use pdfx tool to show all PDF URLs, all URLs (with -v), as well as download all referenced PDFs (using -d):

$ pdfx https://weakdh.org/imperfect-forward-secrecy.pdf -d ./
Reading url 'https://weakdh.org/imperfect-forward-secrecy.pdf'...
Saved pdf as './imperfect-forward-secrecy.pdf'
Document infos:
- CreationDate = D:20150821110623-04'00'
- Creator = LaTeX with hyperref package
- ModDate = D:20150821110805-04'00'
- PTEX.Fullbanner = This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013/Debian) kpathsea version 6.1.1
- Producer = pdfTeX-1.40.14
- Title = Imperfect Forward Secrecy: How Diffie-Hellman Fails in Practice
- Trapped = False
- Pages = 13

Analyzing text...
- URLs: 49
- URLs to PDFs: 17

JSON summary saved as './imperfect-forward-secrecy.pdf.infos.json'

Downloading 17 referenced pdfs...
Created directory './imperfect-forward-secrecy.pdf-referenced-pdfs'
Downloaded 'http://cr.yp.to/factorization/smoothparts-20040510.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/smoothparts-20040510.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35517.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35517.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35514.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35514.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35519.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35519.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35522.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35522.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35509.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35509.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35528.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35528.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35513.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35513.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35533.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35533.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35551.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35551.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35527.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35527.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35520.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35520.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35526.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35526.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35515.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35515.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35529.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35529.pdf'...
Downloaded 'http://cryptome.org/2013/08/spy-budget-fy13.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/spy-budget-fy13.pdf'...
Downloaded 'http://www.spiegel.de/media/media-35671.pdf' to './imperfect-forward-secrecy.pdf-referenced-pdfs/media-35671.pdf'...

You can install it with $ easy_install -U pdfx.

Under the hood, pdfx uses PyPDF2, a Python library to read PDF content and then a regular expression to match all urls.

like image 110
Chris Hager Avatar answered Oct 01 '22 09:10

Chris Hager


strings "somePDFfile.pdf" | grep http

This works even better if you use pdftk to uncompress it first (credits: Ben Stern):

pdftk in.pdf cat output out.pdf uncompress; strings out.pdf | grep -i http
like image 33
ibisum Avatar answered Oct 02 '22 09:10

ibisum