【问题标题】:Is there a way to add "author" metadata to a pdf created from R有没有办法将“作者”元数据添加到从 R 创建的 pdf 中
【发布时间】:2013-09-09 23:07:51
【问题描述】:

在 R 中使用 pdf() 图形设备创建 PDF 时,可以使用 pdf() 的 title= 参数轻松添加标题元数据。但是没有明显的添加作者的方法。

查看 R 中 pdf() 的代码,关键似乎是 C 函数 C_PDF,它显然没有作者参数,并且超出了我的破解能力。有没有其他方法,比将我的图形输出编织成 LaTeX 创建的 PDF 更方便,包括作者信息并节省我们以后手动操作?

.External(C_PDF, file, old$paper, old$family, old$encoding, 
    old$bg, old$fg, old$width, old$height, old$pointsize, 
    onefile, old$pagecentre, old$title, old$fonts, version[1L], 
    version[2L], old$colormodel, old$useDingbats, old$useKerning, 
    old$fillOddEven, old$compress)

我对此不抱太大希望,因为this broader question 没有令人满意的基于语言的答案...

【问题讨论】:

  • 是否可以调用外部程序? exiftool 可以轻松设置生成的 PDF 文件中的元数据
  • 如果可以选择外部程序,pdftk 也可以这样做
  • 您可能会在这里找到答案:stackoverflow.com/questions/24231827
  • 您好,很遗憾,无法使用外部工具,因为我处于锁定网络中,并且打包新应用程序不是此特定任务的优先事项。

标签: r pdf


【解决方案1】:

这里有几个函数可以使用ExifTool 获取和设置任何these filetypes 的Exif 元数据。

获取元数据:

getexif <- function(file, exiftool='exiftool.exe', opts=NULL, 
                    intern=TRUE, simplify=FALSE) {
  # file: the file to be updated
  # exiftool: the path to the ExifTool binary
  # opts: additional arguments to ExifTool (optional)
  # intern: should a named vector of metadata be returned? (bool)
  # simplify: if intern==TRUE, should the results be returned as a named 
  #           vector (TRUE) or as a data.frame (FALSE)?
  arg <- c(opts, normalizePath(file))
  if(intern) {
    exif <- system2(normalizePath(exiftool), args=arg, stdout=TRUE)
    exif <- do.call(rbind, strsplit(exif, ' +: +', perl=T))
    row.names(exif) <- exif[, 1]
    exif[, 2, drop=simplify]
  } else {
    system2(normalizePath(exiftool), args=arg, stdout='')
  }
}

设置元数据:

setexif <- function(file, ..., exiftool='exiftool.exe') {
  # file: the file to be updated
  # ...: metadata items
  # exiftool: the path to the ExifTool binary
  dots <- list(...)
  exif <- sprintf('-%s="%s"', names(dots), dots)
  system2(exiftool, args=c(exif, file))
}

这是一个例子

pdf(f <- tempfile(fileext='.pdf'))
plot(runif(10))
dev.off()

toolpath <- 'c:/software/exiftool(-k).exe'


setexif(f, title = "foo", subject='bar', author = "Me", exiftool=toolpath)

getexif(f, toolpath)

##                            [,1]                                         
## ExifTool Version Number     "9.64"                                       
## File Name                   "file237c6f8d4dac.pdf"                       
## Directory                   "C:/Users/john/AppData/Local/Temp/RtmpSGqI6O"
## File Size                   "7.8 kB"                                     
## File Modification Date/Time "2014:06:17 10:50:22+10:00"                  
## File Access Date/Time       "2014:06:17 10:50:22+10:00"                  
## File Creation Date/Time     "2014:06:17 10:50:20+10:00"                  
## File Permissions            "rw-rw-rw-"                                  
## File Type                   "PDF"                                        
## MIME Type                   "application/pdf"                            
## PDF Version                 "1.4"                                        
## Linearized                  "No"                                         
## Create Date                 "2014:06:17 10:50:20"                        
## Modify Date                 "2014:06:17 10:50:20"                        
## Producer                    "R 3.1.0"                                    
## Creator                     "R"                                          
## Page Count                  "1"                                          
## XMP Toolkit                 "Image::ExifTool 9.64"                       
## Subject                     "bar"                                        
## Title                       "foo"                                        
## Author                      "Me"  

【讨论】:

  • 谢谢,看起来不错,但不幸的是我现在不能使用外部工具。尽管如此,如果我们决定继续前进,这是一个优先事项,至少我知道如何做到这一点。
  • @jbaums 好东西!这是否可以以某种方式应用于已经存在的 PDF 或正在 RMarkdown 中创建的 PDF?
  • @jaySf - setexif 函数将允许您更新已经存在的 pdf 文档(这就是示例所显示的内容)。
  • 好的,但是这个例子展示了如何在某处更新一些临时 PDF,我会有一个带有特定路径的文件。
  • @jbaums 不用再费劲了,我自己发现了。谁有兴趣,只需将上面的f 替换为您的路径,例如。 G。 setexif("C:/myDir/myFile.pdf", c(title = "foo", subject='bar', author = "Me"), toolpath)。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 2011-04-30
  • 2016-12-26
  • 1970-01-01
相关资源
最近更新 更多