使用wkhtmltopdf或标记将data.frames中的html保存为pdf

使用wkhtmltopdf或标记将data.frames中的html保存为pdf,html,r,pdf,r-markdown,wkhtmltopdf,Html,R,Pdf,R Markdown,Wkhtmltopdf,我有一个df,其中有一列htmltext,其中包含我想打印的html文本(如果可能的话,可以成批打印)作为单个PDF,文件名为doc\u id 我可以直接在R内完成吗 我想到了类似的事情 > system("wkhtmltopdf --javascript-delay 1 in.html out.pdf") 我如何在R中实现这一点? 或者有没有其他简单的方法,比如使用降价 # df doc_id <- c("doc1","doc2","doc3") htmltext <-

我有一个df,其中有一列
htmltext
,其中包含我想打印的html文本(如果可能的话,可以成批打印)作为单个PDF,文件名为
doc\u id

我可以直接在R内完成吗

我想到了类似的事情

> system("wkhtmltopdf --javascript-delay 1 in.html out.pdf") 
我如何在R中实现这一点? 或者有没有其他简单的方法,比如使用降价

# df
doc_id <- c("doc1","doc2","doc3")
htmltext <- c("<b>good morning</b>","<b>This text is bold</b>","<b>good evening</b>")
df <- data.frame(doc_id,htmltext, stringsAsFactors = FALSE)

# save htmltext single pdfs with doc_id as filename
filenames = filenames = df$doc_id
...?
#df

doc_id查看其中一项是否可接受:

library(rmarkdown)
library(decapitated) # devtools::install_github("hrbrmstr/decapitated") # requires Chrome

data.frame(
  doc_id = c("doc1", "doc2", "doc3"),
  htmltext = c("<b>good morning</b>", "<b>This text is bold</b>", "<b>good evening</b>"), 
  stringsAsFactors = FALSE
) -> xdf

# hackish pandoc way
for(i in 1:nrow(xdf)) {
  message(sprintf("Processing %s", xdf$doc_id[i]))
  tf <- tempfile(fileext=".html")
  writeLines(xdf$htmltext[i], tf)
  pandoc_convert(
    input = tf, 
    to = "latex", 
    output = sprintf("%s.pdf", xdf$doc_id[i]),
    wd = getwd()
  )
  unlink(tf)
}

# using headless chrome
for(i in 1:nrow(xdf)) {
  message(sprintf("Processing %s", xdf$doc_id[i]))
  tf <- tempfile(fileext=".html")
  writeLines(xdf$htmltext[i], tf)
  chrome_dump_pdf(sprintf("file://%s", tf), path=sprintf("%s.pdf", xdf$doc[i]))
  unlink(tf)
}
库(rmarkdown)
库(无头)#devtools::安装_github(“hrbrmstr/decapitated”)#需要Chrome
数据帧(
doc_id=c(“doc1”、“doc2”、“doc3”),
htmltext=c(“早上好”,“此文本粗体”,“晚上好”),
stringsAsFactors=FALSE
)->xdf
#乌七八糟的潘多克方式
适用于(i/1:nrow(xdf)){
消息(sprintf(“正在处理%s”,xdf$doc_id[i]))
tf