diff --git a/R/send_to_dai.R b/R/send_to_dai.R index bee9400..2351d8d 100644 --- a/R/send_to_dai.R +++ b/R/send_to_dai.R @@ -96,7 +96,7 @@ dai_sync <- function(file, "skipHumanReview" = skip_rev, "rawDocument" = list( "content" = encoded_file, - "mimeType" = "image/tiff" + "mimeType" = "image/png" ) ) diff --git a/R/utils.R b/R/utils.R index b21820c..751626d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -20,27 +20,25 @@ #' # A vector of image files: #' image_to_pdf(images) #' } - image_to_pdf <- function(files, pdf_name) { - # checks if (!(is.vector(files))) { stop("'files' input not a vector.") - } + } if (!(is.character(files))) { stop("'files' input not a character vector.") - } + } if (!(grepl("\\.pdf$", pdf_name))) { stop("Destination filename not .pdf") - } + } # convert magick::image_write(magick::image_read(files), - format = "pdf", - pdf_name - ) + format = "pdf", + pdf_name + ) } #' Check that a file is PDF @@ -54,9 +52,7 @@ image_to_pdf <- function(files, pdf_name) { #' \dontrun{ #' is_pdf("document.pdf") #' } - is_pdf <- function(file) { - result <- suppressMessages(try(pdftools::pdf_info(file), silent = TRUE)) return(!(inherits(result, "try-error"))) @@ -73,13 +69,10 @@ is_pdf <- function(file) { #' \dontrun{ #' is_json("file.json") #' } - is_json <- function(file) { - result <- suppressMessages(try(jsonlite::fromJSON(file), silent = TRUE)) return(!(inherits(result, "try-error"))) - } #' Check that a string is a valid colour representation @@ -94,13 +87,10 @@ is_json <- function(file) { #' is_colour("red") #' is_colour("#12345") #' } - is_colour <- function(x) { - result <- suppressMessages(try(grDevices::col2rgb(x), silent = TRUE)) return(!(inherits(result, "try-error"))) - } #' PDF to base64 tiff @@ -113,12 +103,10 @@ is_colour <- function(x) { #' \dontrun{ #' doc_encoded <- pdf_to_binbase("document.pdf") #' } - pdf_to_binbase <- function(file) { - if (!(is_pdf(file))) { stop("Input file not a pdf.") - } + } img <- magick::image_read_pdf(file) @@ -126,14 +114,12 @@ pdf_to_binbase <- function(file) { filepath <- file.path(tempdir(), "dai_temp.tiff") - magick::image_write(img_gray, - filepath, - format = "tiff"#, - #compression = "JPEG" - ) + magick::image_write(img_gray, + filepath, + format = "png" + ) base64enc::base64encode(filepath) - } #' Image to base64 tiff @@ -146,12 +132,10 @@ pdf_to_binbase <- function(file) { #' \dontrun{ #' img_encoded <- img_to_binbase("image.png") #' } - img_to_binbase <- function(file) { - if (is_pdf(file)) { stop("Input file is .pdf.") - } + } img <- magick::image_read(file) @@ -159,12 +143,10 @@ img_to_binbase <- function(file) { filepath <- file.path(tempdir(), "dai_temp.tiff") - magick::image_write(img_gray, - filepath, - format = "tiff"#, - #compression = "JPEG" - ) + magick::image_write(img_gray, + filepath, + format = "png" + ) base64enc::base64encode(filepath) - }