diff --git a/.Rbuildignore b/.Rbuildignore index f6b8cba..efbfeaf 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -7,3 +7,4 @@ ^revdep$ ^Dockerfile$ ^inst/medias$ +^dev_local diff --git a/.gitignore b/.gitignore index fc3f269..0f9e392 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .Ruserdata revdep Dockerfile +dev_local diff --git a/DESCRIPTION b/DESCRIPTION index 12a3d60..80817d0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: doconv Type: Package Title: Document Conversion to 'PDF' or 'PNG' -Version: 0.3.2 +Version: 0.3.3.0001 Authors@R: c( person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"), @@ -21,7 +21,7 @@ Description: It provides the ability to generate images from documents License: MIT + file LICENSE Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 Imports: magick, pdftools, locatexec, processx, tools Depends: R (>= 4.0.0) Suggests: tinytest, testthat, webshot2 diff --git a/NEWS.md b/NEWS.md index dc5da4b..e7be7b0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,10 @@ +# doconv 0.3.3 (dev version) + +## Issues + +- Fail with informative error message if PowerShell (PS) execution strategy does not allow + running PS scripts. PS scripts are required for certain actions on Windows (#2). + # doconv 0.3.2 ## Features diff --git a/R/docx2pdf.R b/R/docx2pdf.R index 29f8073..5393f7f 100644 --- a/R/docx2pdf.R +++ b/R/docx2pdf.R @@ -61,10 +61,10 @@ docx2pdf <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", input)) } } + #' @importFrom locatexec is_osx #' @importFrom processx run docx2pdf_osx <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", input)){ - if (!is_osx()) { stop("docx2pdf_osx() should only be used on 'macOS' systems.", call. = FALSE) } @@ -101,6 +101,7 @@ docx2pdf_osx <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", inpu output } + #' @importFrom locatexec is_windows docx2pdf_win <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", input)){ @@ -120,18 +121,22 @@ docx2pdf_win <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", inpu output_name <- file.path(default_root, gsub("\\.(docx|doc|rtf)$", ".pdf", basename(input))) - script_sourcefile <- system.file( - package = "doconv", "scripts", "powershell", "docx2pdf.ps1") + script_sourcefile <- system.file(package = "doconv", "scripts", "powershell", "docx2pdf.ps1") script_path <- tempfile(fileext = ".ps1") script_str <- readLines(script_sourcefile, encoding = "UTF-8") script_str[1] <- sprintf(script_str[1], input) script_str[2] <- sprintf(script_str[2], output_name) writeLines(script_str, script_path, useBytes = TRUE) - res <- run("powershell", args = c("-file", script_path), error_on_status = FALSE) + success <- res$status == 0 - if(success) { + # fail with informative error if conversion fails due to PS Execution Policy + if (!success && grepl("UnauthorizedAccess", res$stderr)) { + stop_on_wrong_ps_exec_policy() + } + + if (success) { success <- file.copy(from = output_name, to = output, overwrite = TRUE) } @@ -140,10 +145,9 @@ docx2pdf_win <- function(input, output = gsub("\\.(docx|doc|rtf)$", ".pdf", inpu if(!success) stop("could not convert ", input, res$stderr, call. = FALSE) output - - } + #' @export #' @title Update docx fields #' @description Update all fields and table of contents of a @@ -178,6 +182,7 @@ docx_update <- function(input) { invisible(x) } + docx_update_osx <- function(input){ if (!is_osx()) { @@ -203,6 +208,8 @@ docx_update_osx <- function(input){ success } + + docx_update_win <- function(input){ if (!is_windows()) { @@ -227,16 +234,17 @@ docx_update_win <- function(input){ res <- run("powershell", args = c("-file", script_path), error_on_status = FALSE) success <- res$status == 0 - if(!success) stop("could not update ", input, call. = FALSE) + # fail with informative error if conversion fails due to PS Execution Policy + if (!success && grepl("UnauthorizedAccess", res$stderr)) { + stop_on_wrong_ps_exec_policy() + } + + if (!success) stop("could not update ", input, call. = FALSE) success } - - - - #' @export #' @title Convert pptx to pdf #' @description Convert pptx to pdf directly using "Microsoft PowerPoint". @@ -290,6 +298,7 @@ pptx2pdf <- function(input, output = gsub("\\.pptx$", ".pdf", input)) { } } + #' @importFrom locatexec is_osx #' @importFrom processx run pptx2pdf_osx <- function(input, output = gsub("\\.pptx$", ".pdf", input)){ @@ -332,6 +341,7 @@ pptx2pdf_osx <- function(input, output = gsub("\\.pptx$", ".pdf", input)){ output } + #' @importFrom locatexec is_windows pptx2pdf_win <- function(input, output = gsub("\\.pptx$", ".pdf", input)){ @@ -357,10 +367,12 @@ pptx2pdf_win <- function(input, output = gsub("\\.pptx$", ".pdf", input)){ res <- run("powershell", args = c("-file", script_path), error_on_status = FALSE) success <- res$status == 0 - if(!success) stop("could not convert ", input, call. = FALSE) - - success + # fail with informative error if conversion fails due to PS Execution Policy + if (!success && grepl("UnauthorizedAccess", res$stderr)) { + stop_on_wrong_ps_exec_policy() + } + if(!success) stop("could not convert ", input, call. = FALSE) + output } - diff --git a/R/expect_snapshot_doc.R b/R/expect_snapshot_doc.R index 28e80eb..022bb38 100644 --- a/R/expect_snapshot_doc.R +++ b/R/expect_snapshot_doc.R @@ -69,6 +69,7 @@ expect_snapshot_doc <- function( } + #' @title Visual test for an HTML document #' @description This expectation can be used with 'tinytest' and 'testthat' #' to check if a current document of type HTML diff --git a/R/to_pdf.R b/R/to_pdf.R index 9807b05..ef8696a 100644 --- a/R/to_pdf.R +++ b/R/to_pdf.R @@ -153,7 +153,7 @@ check_libreoffice_export <- function(UserInstallation = NULL) { "--outdir", default_root, input), error_on_status = FALSE, - timeout = 15 + timeout = 45 ) success <- res$status == 0 diff --git a/R/utils.R b/R/utils.R index 025ffeb..e00cb11 100644 --- a/R/utils.R +++ b/R/utils.R @@ -145,3 +145,22 @@ images_to_miniature <- function(img_list, row = NULL, width = 650, img_stack } +# get PS exec policy +ps_execution_policy <- function() { + res <- run("powershell", args = c("Get-ExecutionPolicy"), error_on_status = FALSE) + policy <- gsub("\\s+", "", res$stdout) + policy +} + + +# check if policy allows PS script execution. If not advise user. +stop_on_wrong_ps_exec_policy <- function() { + policy <- ps_execution_policy() + stop("Conversion failed.\n", + "Your PowerShell execution policy '", policy, "' prevents script execution.\n", + "Run one of the following commands in PS as admin to enable script execution: \n", + " `Set-ExecutionPolicy Bypass`\n", + " `Set-ExecutionPolicy Unrestricted`\n", + call. = FALSE + ) +}