How to read PDF metadata from R

吃可爱长大的小学妹 提交于 2019-12-05 01:32:26

I can't think of a pure R way to do this, but you can probably install your favorite PDF command-line tool (for example, the PDF toolkit, PDFtk and use that to get at least some of the data you are looking for.

The following is a basic example using PDFtk. It assumes that pdftk is accessible in your path.

x <- getwd() ## I'll run this example in a tempdir to keep things clean
setwd(tempdir())
list.files(pattern="*.txt$|*.pdf$")
# character(0)

pdf(file = "SomeOutputFile.pdf")
plot(rnorm(100))
dev.off()

system("pdftk SomeOutputFile.pdf data_dump output SomeOutputFile.txt")
list.files(pattern="*.txt$|*.pdf$")
# [1] "SomeOutputFile.pdf" "SomeOutputFile.txt"

readLines("SomeOutputFile.txt")
#  [1] "InfoBegin"                    "InfoKey: Creator"            
#  [3] "InfoValue: R"                 "InfoBegin"                   
#  [5] "InfoKey: Title"               "InfoValue: R Graphics Output"
#  [7] "InfoBegin"                    "InfoKey: Producer"           
#  [9] "InfoValue: R 3.0.1"           "InfoBegin"                   
# [11] "InfoKey: ModDate"             "InfoValue: D:20131102170720" 
# [13] "InfoBegin"                    "InfoKey: CreationDate"       
# [15] "InfoValue: D:20131102170720"  "NumberOfPages: 1"            
# [17] "PageMediaBegin"               "PageMediaNumber: 1"          
# [19] "PageMediaRotation: 0"         "PageMediaRect: 0 0 504 504"  
# [21] "PageMediaDimensions: 504 504"

setwd(x)

I'd look into what other options there are to specify what metadata gets extracted, and see if there's a convenient way to parse this information into a form that is more useful for you.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!