Browse Source

added convert_to_pdf function

pull/21/head
muschellij2 5 years ago
parent
commit
31894f175b
  1. 1
      NAMESPACE
  2. 42
      R/convert_pptx_to_pdf.R
  3. 14
      R/utils.r
  4. BIN
      inst/examples/ex.pptx
  5. 24
      man/convert_pptx_to_pdf.Rd

1
NAMESPACE

@ -3,6 +3,7 @@
S3method(print,docx)
export("%>%")
export(assign_colnames)
export(convert_pptx_to_pdf)
export(docx_cmnt_count)
export(docx_describe_cmnts)
export(docx_describe_tbls)

42
R/convert_pptx_to_pdf.R

@ -0,0 +1,42 @@
#' Convert a PowerPoint Document to a PDF
#'
#' @md
#' @param path path to the PowerPoint document
#' @param pdf_file output PDF file name. By default, creates . This functionality requires the use of
#' LibreOffice and the `soffice` binary it contains. See
#' [set_libreoffice_path] for more information. Note,
#' @export
#' @examples
#' \dontrun{
#' path = system.file("examples/ex.pptx", package="docxtractr")
#' pdf <- convert_pptx_to_pdf(path, pdf_file = tempfile(fileext = ".pdf"))
#' }
convert_pptx_to_pdf <- function(path, pdf_file = sub("[.]pptx", ".pdf", path)) {
stopifnot(is_pptx(path))
lo_path <- getOption("path_to_libreoffice")
if (is.null(lo_path)) {
lo_path <- lo_find()
}
if (is.null(lo_path)) {
stop(lo_path_missing, call. = FALSE)
}
# making temporary file because by default soffice
# will make sub("[.]pptx", ".pdf", path) output
# and don't want to do that in case pdf_file in other location
cp_path = tempfile(fileext = ".pptx")
cp_pdf = sub("[.]pptx", ".pdf", cp_path)
file.copy(path, cp_path)
if (Sys.info()["sysname"] == "Windows") {
convert_win(lo_path, dirname(cp_path), cp_path, convert_to = "pdf")
} else {
convert_osx(lo_path, dirname(cp_path), cp_path, convert_to = "pdf")
}
if (!file.exists(cp_pdf)) {
stop("Conversion from PPTX to PDF did not succeed")
}
file.copy(cp_pdf, pdf_file)
return(pdf_file)
}

14
R/utils.r

@ -33,6 +33,8 @@ is_url <- function(path) { grepl("^(http|ftp)s?://", path) }
is_docx <- function(path) { tolower(tools::file_ext(path)) == "docx" }
is_pptx <- function(path) { tolower(tools::file_ext(path)) == "pptx" }
is_doc <- function(path) { tolower(tools::file_ext(path)) == "doc" }
# Copy a file to a new location, throw an error if the copy fails.
@ -56,18 +58,22 @@ convert_doc_to_docx <- function(docx_dir, doc_file) {
}
# .docx to .doc convertion for Windows
convert_win <- function(lo_path, docx_dir, doc_file) {
cmd <- sprintf('"%s" -convert-to docx:"MS Word 2007 XML" -headless -outdir "%s" "%s"',
convert_win <- function(lo_path, docx_dir, doc_file,
convert_to = 'docx:"MS Word 2007 XML"') {
cmd <- sprintf('"%s" --convert-to %s -headless -outdir "%s" "%s"',
lo_path,
convert_to,
docx_dir,
doc_file)
system(cmd, show.output.on.console = FALSE)
}
# .docx to .doc convertion for OSX
convert_osx <- function(lo_path, docx_dir, doc_file) {
cmd <- sprintf('"%s" --convert-to docx:"MS Word 2007 XML" --headless --outdir "%s" "%s"',
convert_osx <- function(lo_path, docx_dir, doc_file,
convert_to = 'docx:"MS Word 2007 XML"') {
cmd <- sprintf('"%s" --convert-to %s --headless --outdir "%s" "%s"',
lo_path,
convert_to,
docx_dir,
doc_file)
res <- system(cmd, intern = TRUE)

BIN
inst/examples/ex.pptx

Binary file not shown.

24
man/convert_pptx_to_pdf.Rd

@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/convert_pptx_to_pdf.R
\name{convert_pptx_to_pdf}
\alias{convert_pptx_to_pdf}
\title{Convert a PowerPoint Document to a PDF}
\usage{
convert_pptx_to_pdf(path, pdf_file = sub("[.]pptx", ".pdf", path))
}
\arguments{
\item{path}{path to the PowerPoint document}
\item{pdf_file}{output PDF file name. By default, creates . This functionality requires the use of
LibreOffice and the \code{soffice} binary it contains. See
\link{set_libreoffice_path} for more information. Note,}
}
\description{
Convert a PowerPoint Document to a PDF
}
\examples{
\dontrun{
path = system.file("examples/ex.pptx", package="docxtractr")
pdf <- convert_pptx_to_pdf(path, pdf_file = tempfile(fileext = ".pdf"))
}
}
Loading…
Cancel
Save