(Windows 7 / R 版本 3.0.1)
下面的命令和产生的错误:
> library(tm)
> pdf <- readPDF(PdftotextOptions = "-layout")
> dat <- pdf(elem = list(uri = "17214.pdf"), language="de", id="id1")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp
\RtmpS8Uql1\pdfinfo167c2bc159f8': No such file or directory
我该如何解决这个问题?
编辑我
(如 Ben 所建议并在此处描述)
我下载了Xpdf,将 32 位版本复制到
C:\Program Files (x86)\xpdf32
,将 64 位版本复制到
C:\Program Files\xpdf64
环境变量pdfinfo
和pdftotext
指的是各自的可执行文件 32 位(用 R 32 位测试)或 64 位(用 R 64 位测试)
编辑二
一个非常令人困惑的观察是,从一个新的会话(tm 未加载)开始,仅最后一个命令就会产生错误:
> dat <- pdf(elem = list(uri = "17214.pdf"), language="de", id="id1")
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp\RtmpKi5GnL
\pdfinfode8283c422f': No such file or directory
我根本不明白这一点,因为函数变量尚未由 tm.readPDF 定义。下面你会发现函数 pdf 指的是“自然”以及 tm.readPDF 返回的内容:
> pdf
function (elem, language, id)
{
meta <- tm:::pdfinfo(elem$uri)
content <- system2("pdftotext", c(PdftotextOptions, shQuote(elem$uri),
"-"), stdout = TRUE)
PlainTextDocument(content, meta$Author, meta$CreationDate,
meta$Subject, meta$Title, id, meta$Creator, language)
}
<environment: 0x0674bd8c>
> library(tm)
> pdf <- readPDF(PdftotextOptions = "-layout")
> pdf
function (elem, language, id)
{
meta <- tm:::pdfinfo(elem$uri)
content <- system2("pdftotext", c(PdftotextOptions, shQuote(elem$uri),
"-"), stdout = TRUE)
PlainTextDocument(content, meta$Author, meta$CreationDate,
meta$Subject, meta$Title, id, meta$Creator, language)
}
<environment: 0x0c3d7364>
显然没有区别 - 那么为什么要使用 readPDF 呢?
编辑三
pdf 文件位于此处:C:\Users\Raffael\Documents
> getwd()
[1] "C:/Users/Raffael/Documents"
编辑四
第一条指令pdf()
是调用tm:::pdfinfo()
- 并且错误是在前几行中引起的:
> outfile <- tempfile("pdfinfo")
> on.exit(unlink(outfile))
> status <- system2("pdfinfo", shQuote(normalizePath("C:/Users/Raffael/Documents/17214.pdf")),
+ stdout = outfile)
> tags <- c("Title", "Subject", "Keywords", "Author", "Creator",
+ "Producer", "CreationDate", "ModDate", "Tagged", "Form",
+ "Pages", "Encrypted", "Page size", "File size", "Optimized",
+ "PDF version")
> re <- sprintf("^(%s)", paste(sprintf("%-16s", sprintf("%s:",
+ tags)), collapse = "|"))
> lines <- readLines(outfile, warn = FALSE)
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'C:\Users\Raffael\AppData\Local\Temp\RtmpquRYX6\pdfinfo8d419174450': No such file or direc
显然tempfile()
根本不创建文件。
> outfile <- tempfile("pdfinfo")
> outfile
[1] "C:\\Users\\Raffael\\AppData\\Local\\Temp\\RtmpquRYX6\\pdfinfo8d437bd65d9"
该文件夹C:\Users\Raffael\AppData\Local\Temp\RtmpquRYX6
存在并包含一些文件,但没有一个名为pdfinfo8d437bd65d9
.