'How to add space on the left and right of a table exported to a PDF with datatable in shiny?

I want to export a table displayed in a shiny app using datatable to a PDF. For this purpose, I am using the export button provided by datatable, see the code below.

However, I want to make a little modification to the table shown in the PDF. I want to add some space to the left of the first column and to the right of the last column.

I couldn't find a datatable option to control this. It seems, I have to use the pdfmake library to specify this via the customize argument. You can see something I tried in the code below, however commented out, as it does not work. I tried a lot of other stuff but nothing worked. Unfortunately, I have almost no experience with JavaScript, that's why I hope someone can help me here.

library(shiny)
library(DT) 

words <- c("water", "apple", "house", "family", "basket")
set.seed(42)
data <- data.frame(column1 = sample(words, 10, TRUE), 
                   column2 = sample(words, 10, TRUE), 
                   column3 = sample(words, 10, TRUE))

ui <- fluidPage(
  DTOutput("dtable")
)

server <- function(input, output, session) {
  output$dtable <- renderDT({
    datatable(data, 
              rownames = FALSE, 
              extensions = "Buttons", 
              options = list(
                dom = "Bfrtip", 
                buttons = list(
                  list(
                    extend = "pdf", 
                    exportOptions = list(orthogonal = "export"), 
                    filename = "Filename", 
                    title = "Title", 
                    # customize = JS("function(doc) { doc.content.table.margin = [50, 0, 50, 0]; }")
                    orientation = "landscape")
                )
              )
    )
  })
}

shinyApp(ui, server)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source