R脚本-PDF错误:十六进制字符串中存在非法字符;当我搜索关键字时



我正在计算多个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)

如果你有什么建议,请告诉我。非常感谢。

我猜您的pdf被格式化为二进制文件,因此应该被下载/读取为二进制文件。我在使用download.file下载pdf文件时遇到了类似的问题。下载后,我无法使用pdftools从pdf中挖掘信息。我发现我的pdf文件哪里是二进制文件,哪里是坏的bc我没有以正确的格式下载它们(试着使用任何pdf阅读器,打开pdf时应该说它坏了(。使用Windows作为操作系统,我将mode="wb"添加到download.file中,确保它以正确的格式存储它们。然后,我就可以在上面运行pdftools中的函数,而不会出现错误消息。希望这能有所帮助。从SO的问题中得到了这个想法:使用R 下载pdf文件的问题

与您的错误信息相同:

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.