简体   繁体   English

有没有办法将“作者”元数据添加到从 R 创建的 pdf 中

[英]Is there a way to add “author” metadata to a pdf created from R

When creating a PDF with the pdf() graphics device in R, it is possible to add title metadata easily with the title= argument to pdf().在 R 中使用 pdf() 图形设备创建 PDF 时,可以使用 pdf() 的 title= 参数轻松添加标题元数据。 But there is no obvious way of adding an author.但是没有明显的方法可以添加作者。

Looking at the code for pdf() in R, the key seems to be the C function C_PDF, which apparently does not have an author argument and which is beyond my capacity to hack.查看 R 中 pdf() 的代码,关键似乎是 C 函数 C_PDF,它显然没有作者参数,而且超出了我的破解能力。 Is there some other way, more convenient than knitting my graphics output into a LaTeX-created PDF, of including author information and saving us doing it manually later?有没有其他方法比将我的图形输出编织到 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)

I don't have much hope of this as there was no satisfactory language-based answer to this broader question ...我对此不抱太大希望,因为对于这个更广泛的问题没有令人满意的基于语言的答案......

Here are a couple of functions that get and set Exif metadata for any of these filetypes , using ExifTool .以下是使用ExifTool获取和设置任何这些文件类型的Exif 元数据的几个函数。

To get metadata:获取元数据:

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='')
  }
}

To set metadata:设置元数据:

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))
}

Here's an example这是一个例子

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"  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM