为 13K pdf 文档运行 for 循环时出现空间不足错误

Bak*_*kov 6 r batch-processing

我正在为 13K pdf 文件做 for 循环,在其中读取、预处理文本、查找相似性并写入 txt。但是,当我运行 for 循环时,它给出了一个错误

Error in poppler_pdf_text(loadfile(pdf), opw, upw) : Not enough space

原因是什么?

  1. 我试了加memory_limit(),也不是这个问题。
  2. 我试图删除文件夹中的隐藏文件,例如Thumbs.db,但同样的问题再次出现。
  3. 我在每次迭代时删除 pdf 文件。

folder_path <- "C: ...."
## get vector with all pdf names
pdf_folder <- list.files(folder.path)

## for loop over all pdf documents
for(s in 1:length(pdf_folder)){

   ## choose one pdf document from vector of strings
   pdf_document_name <- pdf_folder[s]

   ## read pdf_document pdf into data.frame
   pdf <- read_pdf(paste0(folder_path,"/",pdf_document_name))

   print(s)

   rm(pdf)

} ## end of for loop

# Error: 

Error in poppler_pdf_text(loadfile(pdf), opw, upw) : Not enough space

Run Code Online (Sandbox Code Playgroud)

预期的结果是读取原始路径中的所有 pdf 文档。

And*_*rew 5

我能够通过以下方式重现此错误:

  • 基于图像的 pdf(16,702 页,161,277 KB)
  • R v3.5.3 64 位
  • 文本阅读器 v0.90
  • pdftools v2.2
  • 超立方体 v4.0
  • 视窗 10 64 位
  • 16 GB 内存

这是通过将pdftools包更新到v2.3.1来解决的。

large_pdf_file <- "path/to/file.pdf"

system.time(test <- textreadr::read_pdf(large_pdf_file))
#    user  system elapsed
#  165.64    0.42  166.17

dim(test)
# [1] 519871      3
Run Code Online (Sandbox Code Playgroud)

问题是 pdftools 包使用的 poppler 库中可能存在内存泄漏

使用该textreadr::read_pdf功能读取基于大图像的 pdf 文件时,任务管理器显示 RAM 大幅增加。

如果您坚持使用旧版本的 pdftools,一些用户报告此解决方法成功- 但是,我尝试使用与以前相同的大 pdf 文件并收到此错误:

pdf <- callr::r(function(){
    textreadr::read_pdf('filename.pdf')
})
   
Error in value[[3L]](cond) : 
  callr subprocess failed: could not start R, exited with non-zero status,
has crashed or was killed
Run Code Online (Sandbox Code Playgroud)