Extracting embedded hyperlinks from PDF file

Viewed 564

I am currently working with PDF files in R. Specifically, I need to collect information related with hyperlinks embedded in the text of the PDF file. What I have done so far was parsing the PDF file using the pdftools package. Yet, it renders only the text but not the underlying hyperlinks. Below you can find an example of a PDF file with two embedded hyperlinks (the words "sentencia" and "aqui") and the code I have used.

library(dplyr)
txt <- pdftools::pdf_text(pdf = "http://www.corteidh.or.cr/docs/comunicados/cp_47_17.pdf") %>%
    paste(., collapse = "") %>%
    stringr::str_replace_all("[[:cntrl:]]", " ")

I have also tried:

  1. the step above also after downloading the files
  2. pdftools::pdf_attachments() which gives me an empty list
  3. as well as pdftools::pdf_info() also without success.

I could do it by hand but the corpus is quite large and so, before going in that direction, does anybody know how I could identify and extract embedded hyperlinks from PDF files using this package or anything else?

Many thanks!

1 Answers

Normally, you can read the html links in a PDF if you read the PDF as a txt file. Hence, by reading the content of the PDF with the readr package, we can identify the html links using regular expressions. Here is an example :

library(readr)
library(stringr)
txt <- read_file_raw("C:/cp_47_17.pdf")
nb_Txt <- length(txt)
vector_Txt <- rep("", nb_Txt)

for(i in 1 : nb_Txt)
{
  print(i)
  vector_Txt[i] <- rawToChar(txt[i])
}

text <- paste0(vector_Txt, collapse = "")
position <- stringr::str_locate_all(text, pattern = "http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+")[[1]]
nb_Links <- nrow(position)
list_Link <- list()

for(i in 1 : nb_Links)
{
  list_Link[[i]] <- stringr::str_sub(text, start = position[i, 1], end = position[i, 2])
}
   
list_Link

[[1]]
[1] "http://www.corteidh.or.cr/)"

[[2]]
[1] "http://www.corteidh.or.cr/)"

[[3]]
[1] "http://www.corteidh.or.cr/docs/comunicados/cp_47_17.pdf)"

[[4]]
[1] "https://www.facebook.com/CorteIDH)"

[[5]]
[1] "http://ns.adobe.com/xap/1.0/<?xpacket"

[[6]]
[1] "http://www.w3.org/1999/02/22-rdf-syntax-ns"

[[7]]
[1] "http://ns.adobe.com/xap/1.0/mm/"

[[8]]
[1] "http://ns.adobe.com/xap/1.0/sType/ResourceRef"

[[9]]
[1] "http://ns.adobe.com/xap/1.0/"

[[10]]
[1] "https://www.facebook.com/CorteIDH)"

[[11]]
[1] "https://twitter.com/"

[[12]]
[1] "http://ns.adobe.com/xap/1.0/<?xpacket"

[[13]]
[1] "http://www.w3.org/1999/02/22-rdf-syntax-ns"

[[14]]
[1] "http://ns.adobe.com/xap/1.0/mm/"

[[15]]
[1] "http://ns.adobe.com/xap/1.0/sType/ResourceRef"

[[16]]
[1] "http://ns.adobe.com/xap/1.0/"

[[17]]
[1] "https://twitter.com/"

[[18]]
[1] "http://www.corteidh.or.cr/index.php/es/al-dia/servicios-de-informacion)"

[[19]]
[1] "http://ns.adobe.com/xap/1.0/<?xpacket"

[[20]]
[1] "http://www.w3.org/1999/02/22-rdf-syntax-ns"

[[21]]
[1] "http://ns.adobe.com/xap/1.0/mm/"

[[22]]
[1] "http://ns.adobe.com/xap/1.0/sType/ResourceRef"

[[23]]
[1] "http://ns.adobe.com/xap/1.0/"

[[24]]
[1] "http://www.corteidh.or.cr/index.php/es/al-dia/servicios-de-informacion)"

[[25]]
[1] "http://www.corteidh.or.cr/)"

[[26]]
[1] "http://www.corteidh.or.cr/docs/casos/articulos/seriec_342_esp.pdf)"

[[27]]
[1] "http://www.corteidh.or.cr/docs/casos/articulos/resumen_342_esp.pdf)"

[[28]]
[1] "http://www.corteidh.or.cr/)"

[[29]]
[1] "http://www.corteidh.or.cr/)"

[[30]]
[1] "http://www.corteidh.or.cr/docs/comunicados/cp_47_17.pdf)"

[[31]]
[1] "https://www.facebook.com/CorteIDH)"

[[32]]
[1] "https://www.facebook.com/CorteIDH)"

[[33]]
[1] "https://twitter.com/"

[[34]]
[1] "https://twitter.com/"

[[35]]
[1] "http://www.corteidh.or.cr/index.php/es/al-dia/servicios-de-informacion)"

[[36]]
[1] "http://www.corteidh.or.cr/index.php/es/al-dia/servicios-de-informacion)"

[[37]]
[1] "http://www.corteidh.or.cr/)"

[[38]]
[1] "http://trk.masterbase.com/v2/MB/43BE8FF8FC213E8AFDF5C6C0803C9A652E6CDFC13A12FDC2E5C09FBFB97DD6E9408FA6607146839AE96FEDD9F5713F152BE58FE3CCB8B1668634E717A3978ED7C8EED4BD39E060B344A56FC3685757C06C9B126DBC220384CFC87FCCB2AF548AA356D747120AD53D6114AB4C4AE8EA30155DB2849E3238BE)"

[[39]]
[1] "http://trk.masterbase.com/v2/MB/43BE8FF8FC213E8AFDF5C6C0803C9A652E6CDFC13A12FDC2E5C09FBFB97DD6E9408FA6607146839A55FECA11A324ED5D48BA2676C114DAF357A5D3DADEDB8B263A674D6B08B83FA84DDF5A489225637078227BBFDF54AEB5CAEADE4351C689F3F5FA0D249DBC27A3C8EED4BD39E060B308BDAE9564459553)"

[[40]]
[1] "http://trk.masterbase.com/v2/MB/43BE8FF8FC213E8AFDF5C6C0803C9A652E6CDFC13A12FDC2E5C09FBFB97DD6E9408FA6607146839A71761C9609B8116B48BA2676C114DAF3654F31140F07355A7094CA8C3D18172C898EF5C0557D35B48D886CC77864DAA0085057CC9BBFCA5537B9BFF952BE91A54DDF5A48922563706577EAC6926D8A8D)"

[[41]]
[1] "http://trk.masterbase.com/v2/MB/43BE8FF8FC213E8AFDF5C6C0803C9A652E6CDFC13A12FDC2E5C09FBFB97DD6E9408FA6607146839A63BDE5C8E85A49EB2BE58FE3CCB8B1668634E717A3978ED7C8EED4BD39E060B3F29E78396CB7ADC526FC78E04ADA9C7425A0E5B485AC3EA0FB9CCF6F937BA723403C1985631A5441B387EB116B9F273ABFA1EF54FDDAEE0831C34ACAAEF604A80088D852DA56A988FAFF7F5C8E8523C8315FA93B1F341137)"

[[42]]
[1] "http://trk.masterbase.com/v2/MB/43BE8FF8FC213E8AFDF5C6C0803C9A652E6CDFC13A12FDC2E5C09FBFB97DD6E9408FA6607146839A13B26E68C26F9692C0FD35395A75F2386E73C2E55D0F1737EE6C58C832C8F9BA04A65C1BB9D0C9106D5EF8BD65DE6A546C9B126DBC220384CFC87FCCB2AF548AA356D747120AD53D6114AB4C4AE8EA30155DB2849E3238BE)"

[[43]]
[1] "http://trk.masterbase.com/v2/MB/43BE8FF8FC213E8AFDF5C6C0803C9A652E6CDFC13A12FDC2E5C09FBFB97DD6E9408FA6607146839A08B590D424E8B9296EE15DE3555B78744954C38A85CE5C46D2D441CD4016DC72B9F5A077A01802BB8D886CC77864DAA0085057CC9BBFCA5537B9BFF952BE91A54DDF5A48922563706577EAC6926D8A8D)"```
Related