首页 > 解决方案 > 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)

如果您有任何建议,请让我知道。谢谢!

标签: rpdftext-mining

解决方案


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

推荐阅读