Fix #310
diff --git a/R/save_kable.R b/R/save_kable.R
index ed36eef..fe80d8f 100644
--- a/R/save_kable.R
+++ b/R/save_kable.R
@@ -41,14 +41,14 @@
     if (attr(x, "format") == "latex") {
       return(save_kable_latex(x, file, latex_header_includes, keep_tex, density))
 
-    # markdown
+      # markdown
     } else if (attr(x, "format") == "pipe") {
 
       # good file extension: write to file
       if (tools::file_ext(file) %in% c("txt", "md", "markdown", "Rmd")) {
         return(save_kable_markdown(x, file))
 
-      # bad file extension: warning + keep going to html writer
+        # bad file extension: warning + keep going to html writer
       } else {
         warning('`save_kable` can only save markdown tables to files with the following extensions: .txt, .md, .markdown, .Rmd. Since the supplied file name has a different extension, `save_kable` will try to use the HTML writer. This is likely to produce suboptimal results. To save images or other file formats, try supplying a LaTeX or HTML table to `save_kable`.')
       }
@@ -102,7 +102,8 @@
     temp_dir <- sub(pattern = '^[\\\\/]{1,2}',
                     replacement = '',
                     tempfile(pattern = 'lib', tmpdir = '' , fileext = ''))
-    save_HTML(html_result, file = file_temp_html, libdir = temp_dir)
+    save_HTML(html_result, file = file_temp_html, libdir = temp_dir,
+              self_contained = FALSE)
 
     result <- webshot::webshot(file_temp_html, file, ...)
     if (is.null(result)) {
@@ -138,13 +139,15 @@
       temp_dir <- sub(pattern = '^[\\\\/]{1,2}',
                       replacement = '',
                       tempfile(pattern = 'lib', tmpdir = '' , fileext = ''))
-      save_HTML(html_result, file = file, libdir = temp_dir)
+      save_HTML(html_result, file = file, libdir = temp_dir,
+                self_contained = TRUE)
       #remove_html_doc(file)
-      rmarkdown::pandoc_self_contained_html(file, file)
+      self_contained(file, file)
       unlink(file.path(dirname(file), temp_dir), recursive = TRUE)
     } else {
-      # Simply use the htmltools::save_html to write out the files. Dependencies go to the standard lib folder
-      htmltools::save_html(html_result, file = file)
+      # Simply use the htmltools::save_html to write out the files.
+      # Dependencies go to the standard lib folder
+      save_HTML(html_result, file = file, self_contained = FALSE)
     }
   }
 
@@ -153,7 +156,7 @@
 
 # Local version of htmltools::save_html with fix to relative path.
 # See https://github.com/rstudio/htmltools/pull/105
-save_HTML <- function(html, file, background = "white", libdir="lib") {
+save_HTML <- function(html, file, libdir = "lib", self_contained = TRUE) {
   base_file <- basename(file)
   dir <- dirname(file)
   file <- file.path(dir, base_file)
@@ -165,16 +168,37 @@
     dep <- htmltools::makeDependencyRelative(dep, dir, FALSE)
     dep
   })
-  html <- c("<!DOCTYPE html>", "<html>", "<head>",
-            "<meta charset=\"utf-8\" title=\"table output\"/>",
-            sprintf("<style>body{background-color:%s;}</style>",
-                    htmltools::htmlEscape(background)),
-            htmltools::renderDependencies(deps, c("href", "file")),
-            rendered$head, "</head>", "<body>",
-            rendered$html, "</body>", "</html>")
+  html <- c(
+    if (self_contained) "" else "<!DOCTYPE html>",
+    "<html>", "<head>",
+    "<meta charset=\"utf-8\"/>",
+    "<title>table output</title>",
+    htmltools::renderDependencies(deps, c("href", "file")),
+    rendered$head, "</head>", "<body>",
+    rendered$html, "</body>", "</html>")
   writeLines(html, file, useBytes = TRUE)
 }
 
+# Local version of rmarkdown::pandoc_self_contained_html(input, output) to
+# remove the no title bug
+self_contained <- function(input, output) {
+  input <- normalizePath(input)
+  if (!file.exists(output))
+    file.create(output)
+  output <- normalizePath(output)
+  template <- tempfile(fileext = ".html")
+  on.exit(unlink(template), add = TRUE)
+  rmarkdown:::write_utf8("$body$", template)
+  from <- if (rmarkdown::pandoc_available("1.17")) "markdown_strict" else "markdown"
+  rmarkdown::pandoc_convert(
+    input = input, from = from, output = output,
+    options = c("--metadata", 'pagetitle="table output"', "--self-contained",
+                "--template", template))
+  invisible(output)
+}
+
+
+
 remove_html_doc <- function(x){
   out <- paste(readLines(x)[-1], collapse = "\n")
   writeLines(out, x)