Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create pdf in addition to word docx using officer

I am using officer (used to use reporters) within a loop to create 150 unique documents. I need these documents however to be exported from R as word docx AND pdfs.

Is there a way to export the document created with officer to a pdf?

like image 507
Amanda Avatar asked Dec 14 '22 14:12

Amanda


1 Answers

That's possible but the solution I have depends on libreoffice. Here is the code I am using. Hope it will help. I've hard-coded libreoffice path then you probably will have to adapt or improve the code for variable cmd_.

The code is transforming a PPTX or DOCX file to PDF.

library(pdftools)
office_shot <- function( file, wd = getwd() ){
  cmd_ <- sprintf(
    "/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to pdf --outdir %s %s",
    wd, file )
  system(cmd_)

  pdf_file <- gsub("\\.(docx|pptx)$", ".pdf", basename(file))
  pdf_file
}
office_shot(file = "your_presentation.pptx")
like image 83
David Gohel Avatar answered Dec 17 '22 22:12

David Gohel