-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from muschellij2/master
ENH: Function to Convert PPTX to PDF
- Loading branch information
Showing
12 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
language: R | ||
sudo: false | ||
sudo: required | ||
cache: packages | ||
|
||
before_install: | ||
- if [[ "${TRAVIS_OS_NAME}" = "osx" ]]; then brew cask install libreoffice ; fi | ||
- if [[ "${TRAVIS_OS_NAME}" = "linux" ]]; then sudo apt-get update -y && sudo apt-get install libreoffice; fi | ||
|
||
after_success: | ||
- Rscript -e 'covr::codecov()' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Convert a Document (usually PowerPoint) to a PDF | ||
#' | ||
#' @md | ||
#' @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 | ||
#' 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_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_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") | ||
|
||
# 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|docx|doc)$", ".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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
context("DOC conversion works") | ||
test_that("we can convert a DOC to DOCX if LibreOffice Installed", { | ||
lp = try({ | ||
docxtractr:::lo_find() | ||
}, silent = TRUE) | ||
if (!inherits(lp, "try-error")) { | ||
path <- system.file("examples/preserve.doc", package = "docxtractr") | ||
doc = read_docx(path) | ||
expect_that(doc, is_a("docx")) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
context("PPTX conversion works") | ||
test_that("we can convert a PPTX if LibreOffice Installed", { | ||
lp = try({ | ||
docxtractr:::lo_find() | ||
}, silent = TRUE) | ||
if (!inherits(lp, "try-error")) { | ||
path <- system.file("examples/ex.pptx", package = "docxtractr") | ||
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) | ||
} | ||
}) | ||
|