diff --git a/NAMESPACE b/NAMESPACE index ad817c5..6305109 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,7 +3,7 @@ S3method(print,docx) export("%>%") export(assign_colnames) -export(convert_pptx_to_pdf) +export(convert_to_pdf) export(docx_cmnt_count) export(docx_describe_cmnts) export(docx_describe_tbls) diff --git a/R/convert_pptx_to_pdf.R b/R/convert_pptx_to_pdf.R index eeb890a..7d8abb9 100644 --- a/R/convert_pptx_to_pdf.R +++ b/R/convert_pptx_to_pdf.R @@ -1,7 +1,7 @@ -#' Convert a PowerPoint Document to a PDF +#' Convert a Document (usually PowerPoint) to a PDF #' #' @md -#' @param path path to the PowerPoint document +#' @param path path to the document, can be PowerPoint or DOCX #' @param pdf_file output PDF file name. By default, creates a PDF in the #' same directory as the `path` file. #' This functionality requires the use of @@ -11,10 +11,12 @@ #' @examples #' \dontrun{ #' path = system.file("examples/ex.pptx", package="docxtractr") -#' pdf <- convert_pptx_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) +#' pdf <- convert_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) +#' path = system.file("examples/data.docx", package="docxtractr") +#' pdf_doc <- convert_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) #' } -convert_pptx_to_pdf <- function(path, pdf_file = sub("[.]pptx", ".pdf", path)) { - stopifnot(is_pptx(path)) +convert_to_pdf <- function(path, pdf_file = sub("[.]pptx", ".pdf", path)) { + stopifnot(is_pptx(path) | is_doc(path) | is_docx(path)) lo_assert() lo_path <- getOption("path_to_libreoffice") @@ -23,7 +25,7 @@ convert_pptx_to_pdf <- function(path, pdf_file = sub("[.]pptx", ".pdf", path)) { # 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) + cp_pdf = sub("[.](pptx|docx|doc)$", ".pdf", cp_path) file.copy(path, cp_path) if (Sys.info()["sysname"] == "Windows") { diff --git a/man/convert_pptx_to_pdf.Rd b/man/convert_to_pdf.Rd similarity index 51% rename from man/convert_pptx_to_pdf.Rd rename to man/convert_to_pdf.Rd index 138d99f..1f6da50 100644 --- a/man/convert_pptx_to_pdf.Rd +++ b/man/convert_to_pdf.Rd @@ -1,13 +1,13 @@ % 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} +\name{convert_to_pdf} +\alias{convert_to_pdf} +\title{Convert a Document (usually PowerPoint) to a PDF} \usage{ -convert_pptx_to_pdf(path, pdf_file = sub("[.]pptx", ".pdf", path)) +convert_to_pdf(path, pdf_file = sub("[.]pptx", ".pdf", path)) } \arguments{ -\item{path}{path to the PowerPoint document} +\item{path}{path to the document, can be PowerPoint or DOCX} \item{pdf_file}{output PDF file name. By default, creates a PDF in the same directory as the \code{path} file. @@ -16,11 +16,13 @@ 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 +Convert a Document (usually PowerPoint) to a PDF } \examples{ \dontrun{ path = system.file("examples/ex.pptx", package="docxtractr") -pdf <- convert_pptx_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) +pdf <- convert_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) +path = system.file("examples/data.docx", package="docxtractr") +pdf_doc <- convert_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) } } diff --git a/tests/testthat/test-pptx-conversion.R b/tests/testthat/test-pptx-conversion.R index 0f805d6..d8b3b8e 100644 --- a/tests/testthat/test-pptx-conversion.R +++ b/tests/testthat/test-pptx-conversion.R @@ -5,7 +5,18 @@ test_that("we can convert a PPTX if LibreOffice Installed", { }, silent = TRUE) if (!inherits(lp, "try-error")) { path <- system.file("examples/ex.pptx", package = "docxtractr") - pdf <- convert_pptx_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) + pdf <- convert_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) + expect_true(file.size(pdf) > 0) + } +}) + +test_that("we can convert a DOCX to PDF if LibreOffice Installed", { + lp = try({ + docxtractr:::lo_find() + }, silent = TRUE) + if (!inherits(lp, "try-error")) { + path <- system.file("examples/data.docx", package = "docxtractr") + pdf <- convert_to_pdf(path, pdf_file = tempfile(fileext = ".pdf")) expect_true(file.size(pdf) > 0) } })