我正在尝试计算多个pdf文件中关键字的数量。
library(tm)
library(pdftools)
files <- list.files(pattern = "pdf$")
Rpdf <- readPDF(control = list(text = "-layout"))
corp <- Corpus(URISource(files), readerControl = list(reader = Rpdf))
words <- c("example", "keyword", "test")
dt <- DocumentTermMatrix(corp, control=list(dictionary=words))
运行代码时,我总是会遇到此错误:
PDF error: May not be a PDF file (continuing anyway)
PDF error (3): Illegal character <21> in hex string
PDF error (5): Illegal character <4f> in hex string
PDF error (7): Illegal character <54> in hex string
PDF error (8): Illegal character <59> in hex string
PDF error (9): Illegal character <50> in hex string
PDF error: Couldn't find trailer dictionary
PDF error: Couldn't find trailer dictionary
PDF error: Couldn't read xref table
Error in poppler_pdf_text(loadfile(pdf), opw, upw) : PDF parsing failure.
In addition: There were 12 warnings (use warnings() to see them)
如果您有任何建议,请告诉我。谢谢!
答案 0 :(得分:1)
我想您的pdf格式为二进制文件,因此应该作为二进制文件下载/读取。我在使用download.file
下载pdf文件时遇到了类似的问题。下载它们后,我无法使用pdftools
从pdf挖掘信息。我发现我的pdf包含二进制文件和不完整的BC,但我没有以正确的格式下载它们(尝试使用任何pdf阅读器,打开pdf时应该说它已损坏)。使用Windows作为操作系统,我向mode="wb"
添加了download.file
,以确保它以正确的格式存储它们。然后,我可以从pdftools
上运行功能,而不会收到该错误消息。希望能有所帮助。从这样的问题中得到了一个主意:Problems with Downloading pdf file using R
与您的错误消息相同:
pdf_toc(example_path)
PDF error (1151926): Illegal character <3a> in hex string
PDF error (1151929): Illegal character <73> in hex string
[...omitted for brevity...]
PDF error (1152006): Illegal character <22> in hex string
PDF error: Couldn't find trailer dictionary
PDF error: Couldn't read xref table
Error in poppler_pdf_toc(loadfile(pdf), opw, upw) : PDF parsing failure.