【问题标题】:save html from data.frames to pdf using wkhtmltopdf or Markdown使用 wkhtmltopdf 或 Markdown 将 html 从 data.frames 保存为 pdf
【发布时间】:2018-06-19 02:38:25
【问题描述】:

我有一个带有 htmltext 列的 df,其中包含我想以 doc_id 作为文件名的单个 PDF 打印(如果可能的话,作为批处理)作为单个 PDF 的 html 文本。

我可以直接在 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
...?

【问题讨论】:

    标签: html r pdf r-markdown wkhtmltopdf


    【解决方案1】:

    看看其中一个是否可以接受:

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

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 2017-02-09
      • 2014-03-23
      相关资源
      最近更新 更多