如何解决在 R 中阅读 PDF 组合
是否可以在 R 中读取/转换 PDF 组合?
library(pdftools)
#> Using poppler version 0.73.0
link <- c("http://www.accessdata.fda.gov/cdrh_docs/pdf19/K190072.pdf")
pdftools::pdf_convert(link,dpi = 600)
#> Converting page 1 to K190072_1.png...
#> PDF error: Non conformant codestream TPsot==TNsot.<0a>
#> PDF error: Non conformant codestream TPsot==TNsot.<0a>
#> PDF error: Non conformant codestream TPsot==TNsot.<0a>
#> PDF error: Non conformant codestream TPsot==TNsot.<0a>
#> done!
#> [1] "K190072_1.png"
由 reprex package (v1.0.0) 于 2021 年 5 月 6 日创建
我最终得到的 K190072_1.png
只是投资组合首页的图像。
我对此 PDF 组合的文档 K190072.510kSummary.Final_Sent001.pdf
感兴趣
我为 Python (Reading a PDF Portfolio in Python?) 找到了一种方法,但我真的很想在 R 中做到这一点。
感谢您的帮助。
解决方法
pdf_convert
处理一页原始 pdf 数据似乎存在问题(它想在这些条件下使用 basename(pdf)
),所以我编辑了该函数,以便它也适用于第二个附加的pdf文件。
如果您只需要第一个文件,那么您可以使用原始的 pdf_convert
函数运行它,但是第二个文件会出错。
如果您有兴趣从对我有用的附件中渲染光栅图形:
library(pdftools)
#> Using poppler version 21.02.0
link <- c("http://www.accessdata.fda.gov/cdrh_docs/pdf19/K190072.pdf")
pdf_convert <- function (pdf,format = "png",pages = NULL,filenames = NULL,dpi = 72,antialias = TRUE,opw = "",upw = "",verbose = TRUE) {
config <- poppler_config()
if (!config$can_render || !length(config$supported_image_formats))
stop("You version of libppoppler does not support rendering")
format <- match.arg(format,poppler_config()$supported_image_formats)
if (is.null(pages))
pages <- seq_len(pdf_info(pdf,opw = opw,upw = upw)$pages)
if (!is.numeric(pages) || !length(pages))
stop("Argument 'pages' must be a one-indexed vector of page numbers")
if (length(filenames) < 2 & !is.raw(pdf)) { # added !is.raw(pdf)
input <- sub(".pdf","",basename(pdf),fixed = TRUE)
filenames <- if (length(filenames)) {
sprintf(filenames,pages,format)
}
else {
sprintf("%s_%d.%s",input,format)
}
}
if (length(filenames) != length(pages))
stop("Length of 'filenames' must be one or equal to 'pages'")
antialiasing <- isTRUE(antialias) || isTRUE(antialias ==
"draw")
text_antialiasing <- isTRUE(antialias) || isTRUE(antialias ==
"text")
pdftools:::poppler_convert(pdftools:::loadfile(pdf),format,filenames,dpi,opw,upw,antialiasing,text_antialiasing,verbose)
}
lapply(pdf_attachments(link),function(x) pdf_convert(x$data,filenames=paste0(tools::file_path_sans_ext(x$name),"-",seq_along(pdf_data(x$data)),".png")))
#> Converting page 1 to K190072.510kSummary.Final_Sent001-1.png... done!
#> Converting page 2 to K190072.510kSummary.Final_Sent001-2.png... done!
#> Converting page 3 to K190072.510kSummary.Final_Sent001-3.png... done!
#> Converting page 4 to K190072.510kSummary.Final_Sent001-4.png... done!
#> Converting page 5 to K190072.510kSummary.Final_Sent001-5.png... done!
#> Converting page 1 to K190072.IFU.FINAL_Sent001-1.png... done!
#> Converting page 1 to K190072.Letter.SE.FINAL_Sent001-1.png... done!
#> Converting page 2 to K190072.Letter.SE.FINAL_Sent001-2.png... done!
#> [[1]]
#> [1] "K190072.510kSummary.Final_Sent001-1.png"
#> [2] "K190072.510kSummary.Final_Sent001-2.png"
#> [3] "K190072.510kSummary.Final_Sent001-3.png"
#> [4] "K190072.510kSummary.Final_Sent001-4.png"
#> [5] "K190072.510kSummary.Final_Sent001-5.png"
#>
#> [[2]]
#> [1] "K190072.IFU.FINAL_Sent001-1.png"
#>
#> [[3]]
#> [1] "K190072.Letter.SE.FINAL_Sent001-1.png"
#> [2] "K190072.Letter.SE.FINAL_Sent001-2.png"
由 reprex package (v2.0.0) 于 2021 年 5 月 5 日创建
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。