Search code examples
rexcelxlsxdcom

Print/save Excel (.xlsx) sheet to PDF using R


I want to print an Excel file to a pdf file after manipulating it. For the manipulation I used the .xlsx package which works fine. There is a function printSetup but I cannot find a function to start the printing. Is there a solution for this?

library(xlsx)
file <- "test.xlsx"
wb <- loadWorkbook(file)  
sheets <- getSheets(wb)       # get all sheets
sheet <- sheets[[1]]          # get first sheet
# HERE: MAGIC TO SAVE THIS SHEET TO PDF

It may be a solution using DCOM through the RDCOMClient package, though I would prefer a plattform independent solution (e.g. using xlsx) as I work on MacOS. Any ideas?


Solution

  • An open source and cross platform way to do this would be with libreoffice as so:

    library("XLConnect")
    x <- rnorm(1:100)
    y <- x ^ 2
    writeWorksheetToFile("test.xlsx", data.frame(x = x, y = y), "Data")
    tmpDir <- file.path(tempdir(), "LOConv")
    system2("libreoffice", c(paste0("-env:UserInstallation=file://", tmpDir), "--headless", "--convert-to pdf",
        "--outdir", getwd(), file.path(getwd(),"test.xlsx")))
    

    Ideally you'd then remove the folder referenced by tmpDir but that would be platform specific.

    Note this assumes libreoffice is in your path. If it isn't, then the command would need to be altered to include the full path to the libreoffice executable.

    The reason for the env bit is that headless libreoffice will only do anything otherwise if it isn't already running in GUI mode. See http://ask.libreoffice.org/en/question/1686/how-to-not-connect-to-a-running-instance/ for more info.