#' Output the Graphics to PDF Device
#'
#' @param outpdf The output pdf file name.
#'   When onefie=FALSE, give a C integer format such as "Rplot\%03d.pdf".
#' @param close = 1 to close the pdf device.
#' @param paper Size of the pdf paper: "a4", "letter", "legal"
#'  and "executive", or "a4r" and "USr" for rotated (landscape.)
#' @param height Height of the pdf paper in mm (not inches)
#' @param width Width of the pdf paper  in mm (not inches)
#' @param onefile = FLASE to create many files.
#' @param ... Other arguments to native pdf function.
#'
#' @details
#' This function calls native pdf function as:\cr
#' \code{pdf(outpdf, paper=paper, height=height, width=width}
#' \code{, onefile=onefile, ... )} \cr
#' When paper=NULL or "special", the paper size will be determined by
#' width and hight.
#' \cr
#' The defalt paper size is "a4r" which is equivalent to: \cr
#' \code{paper="special", height=210, width=297}
#'
#' @examples
#' \dontrun{
#' plot(-5:5,(-5:5)^2, type="l", main="square")
#' plot(-5:5,(-5:5)^3, type="l", main="cubic")
#'
#' # put everything in one pdf file
#' graph2pdf("testestestest.pdf")
#' plot(-5:5,(-5:5)^2, type="l", main="square")
#' plot(-5:5,(-5:5)^3, type="l", main="cubic")
#' graph2pdf(close=1)
#'
#' # create one pdf file for each graph with automatic naming
#' graph2pdf("testestestest%03d.pdf", onefile=FALSE)
#' plot(-5:5,(-5:5)^2, type="l", main="square")
#' plot(-5:5,(-5:5)^3, type="l", main="cubic")
#' graph2pdf(close=1)
#'
#' # create one pdf file for each graph with specific name
#' for( i in 1:3 ){
#'  fname=paste("f", i, ".pdf", sep="")
#'  graph2pdf(fname)
#'  plot(-5:5,(-5:5)^i, type="l", main=i)
#' }
#' graph2pdf(close=1)
#'
#' # default paper size: paper="a4r"
#' graph2pdf("test0.pdf")
#' plot(-5:5,(-5:5)^2, type="l", main="test")
#' graph2pdf(close=1)
#'
#' # ??
#' graph2pdf("test00.pdf", paper="a4")
#' plot(-5:5,(-5:5)^2, type="l", main="test")
#' graph2pdf(close=1)
#'
#'
#' # paper size with width and height
#' graph2pdf("test1.pdf", paper="a4r", width=197, height=120)
#' plot(-5:5,(-5:5)^2, type="l", main="test")
#' graph2pdf(close=1)
#'
#' # Below two produces the same pdfs.
#' graph2pdf("test2.pdf", paper=NULL, width=197, height=120)
#' plot(-5:5,(-5:5)^2, type="l", main="test")
#' graph2pdf(close=1)
#'
#' graph2pdf("test3.pdf", paper="special", width=197, height=120)
#' plot(-5:5,(-5:5)^2, type="l", main="test")
#' graph2pdf(close=1)
#'
#' }
#'
#' @export
#'

graph2pdf <- function( outpdf, close=0, paper="a4r"
                     , height=0, width=0, onefile=TRUE,  ... ){
 # draw the graphs on pdf device
 # Shin-ichi Mayekawa
 # 20171106
 # example added: 20191106
 # height and width in cm: 20200205dnc,0206
 #

 if( close == 0 ){

  # delete the pdf graphic device
  devl=dev.list()
  locpdf=which(names(devl) == "pdf")
  if( length(locpdf) > 0 ){
   lapply( locpdf, function(x) dev.off(devl[x]) )
  }
  # open pdf device

    pdf(outpdf, paper=paper
       , height=height/25.4, width=width/25.4, onefile=onefile, ... )

 } else{
  # delete the pdf graphic device
  devl=dev.list()
  locpdf=which(names(devl) == "pdf")
  if( length(locpdf) > 0 ){
   cat("\nClosing devl[locpdf] = ",devl[locpdf],"\n")
   lapply( locpdf, function(x) dev.off(devl[x]) )
  }
 }

} # end of graph2pdf


