[英]How to count the number of pages in a PDF from R?
有沒有辦法從 R 計算 PDF 文件中的頁數? 如果沒有,是否有另一種獨立於操作系統的方法來做到這一點? 到目前為止,我找到的唯一答案是this ,它特定於 Windows 7。
我正在嘗試在 R 和knitr
編譯一些報告,聚合來自先前腳本的 PDF 繪圖輸出,該腳本會自動處理數百個數據集。 一些數據集很糟糕,最終會破壞繪圖功能。 由於 plot 函數包含在pdf
函數中,因此會生成一個空的 PDF 文件,由報告找到並破壞pdflatex
。 嘗試修改分析腳本以避免首先生成這些 PDF 已被證明是困難的,並且是非常特定於案例的。 我真的很想擁有一些可以嵌入到報告中的功能,該功能將在包含 PDF 之前檢查 >=1 頁的 PDF。 我更喜歡基於 R 的解決方案,盡管 bash、LaTeX、 knitr
或 pdflatex 解決方案也可能就足夠了。
編輯:另外,正如我鏈接到的上一個答案中提到的,我嘗試使用Rpoppler
( 此處)但無法編譯。 我在沒有管理員訪問權限的 CentOS 6 環境中使用 R 版本 3.3.0。
其他建議和代碼似乎不必要地不透明或復雜。 安裝pdftools
, pdf_info
命令將返回 pages 字段:
library(pdftools)
# returns number of pages
# assumes your_file_name.pdf is in working directory
pdf_info("your_file_name.pdf")$pages
# to see other available metadata in pdf_info object, use names()
names(pdf_info("your_file_name.pdf"))
下面的腳本對我有用。
#########################################
#GET PDF PAGE NUMBER :: R - JULY 16
##########################################
##SOURCE
#----pdftools package
#https://cran.rstudio.com/web/packages/pdftools
#Requirement
#brew install poppler
## TO AVOID ERROR ::: configure: error: cannot determine poppler-glib compile/link flags
#INSTALL PACKAGES
#install.packages("pdftools", dependencies=TRUE) #only once
#IN/OUT FILES
in_put_pdf="pathTo/test.pdf"
out_put_pdf="pathTo/testCopy.pdf"
#LOAD LIBS
library(pdftools)
#Copy of the original file
file.copy(file.path(Sys.getenv("PATH_TO_PDF_FILE"), in_put_pdf), out_put_pdf)
#Many informations about the file are displayed here
info <- pdf_info(out_put_pdf)
text <- pdf_text(out_put_pdf)
fonts <- pdf_fonts(out_put_pdf)
files <- pdf_attachments(out_put_pdf)
#To get the number of pages
numberOfPageInPdf = info[2]
numberOfPageInPdf
希望能有所幫助。 祝你好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.