Skip to content

Commit

Permalink
Create mni_combinar_docs.R
Browse files Browse the repository at this point in the history
  • Loading branch information
jjesusfilho authored Feb 27, 2024
1 parent 39694e8 commit 6960b88
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions R/mni_combinar_docs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
mni_combinar_docs <- function(arquivos = NULL,
dir_origem = ".",
dir_destino = NULL,
nivel = c("processo", "doc")){

if (is.null(dir_destino) || !dir.exists(dir_destino)){

stop("Voc\u00ea deve informar um diret\u00f3rio existente")

}

if (is.null(arquivos)){

arquivos <- list.files(dir_origem, full.names = TRUE, pattern = "pdf$")

}

nivel = nivel[1]


lista <- tibble::tibble(arquivos) |>
dplyr::mutate(processo = stringr::str_extract(arquivos,"\\d{20}"),
id_documento = stringr::str_extract(arquivos,'(?<=documento_)\\d+')) |>
tidyr::separate_wider_delim(id_documento, delim = stringr::regex("(?=\\d$)"),
names = c("id_documento","digito")) |>
dplyr::mutate(dplyr::across(id_documento:digito, as.integer)) |>
dplyr::arrange(processo,id_documento,digito)

if (nivel == "processo"){

lista <- dplyr::group_split(lista, processo)


purrr::walk(lista, purrr::possibly(~{

processo <- unique(.x$processo)

qpdf::pdf_combine(.x$arquivos, file.path(dir_destino,paste0(processo,".pdf")))

},NULL))


} else {

lista <- dplyr::group_split(lista, processo, id_documento)

purrr::walk(lista, purrr::possibly(~{
processo <- unique(.x$processo)
id_documento <- unique(.x$id_documento)
suppressWarnings(
qpdf::pdf_combine(.x$arquivos, file.path(dir_destino,
paste0(processo,"_id_documento_",id_documento, ".pdf")))
)
}, NULL))
}




}

0 comments on commit 6960b88

Please sign in to comment.