Convert documents to pdf with a script using 'Office' or 'Libre Office'.
If 'Microsoft Word', 'Microsoft PowerPoint' and 'Microsoft Excel' are available, files 'docx', 'doc', 'rtf', 'pptx', 'ppt', 'xlsx' and 'xlsm' will be converted to PDF with 'Office' via a script.
If they are not available (on linux for example), 'Libre Office' will be used to convert documents. In that case the rendering can be different from the original document. It supports very well 'Microsoft PowerPoint' to PDF. 'Microsoft Word' can also be converted but some Word features are not supported, such as sections.
Workbooks ('xlsx', 'xlsm', 'xls', 'ods') are exported sheet by sheet:
'Excel' scales each sheet to the width of one page, see xlsx2pdf();
'LibreOffice' exports each sheet on a single page with the size of
its content.
Usage
to_pdf(
input,
output = gsub("\\.[[:alnum:]]+$", ".pdf", input),
timeout = 120,
UserInstallation = NULL,
show_markup = FALSE
)Arguments
- input, output
file input and optional file output. If output file is not provided, the value will be the value of input file with extension 'pdf'.
- timeout
timeout in seconds that 'LibreOffice' is allowed to use to convert the document, ignored if 0. It is not used when 'Microsoft Office' converts the document.
- UserInstallation
use this value to set a non-default user profile path for 'LibreOffice'. If not provided a temporary dir is created. It makes possibles to use more than a single session of 'LibreOffice'.
- show_markup
if
TRUE, tracked changes and comments of a 'Word' document are shown in the PDF. IfFALSE(the default), the PDF shows the final document, without tracked changes marks and without comments. It is ignored for other documents. With 'LibreOffice', comments are drawn in the margin, and hiding tracked changes is only supported for 'docx' files and requires package 'officer'.
Linux platforms
R adds its own library directories to the environment variable
LD_LIBRARY_PATH, which made 'LibreOffice' fail when run from R
(with the message "libreglo.so cannot open shared object file", or
an abort when /usr/lib/libreoffice/program was added to this
variable). 'LibreOffice' is now run without this variable, there is
nothing to set.
Examples
library(locatexec)
if (exec_available("libreoffice") && check_libreoffice_export()) {
out_pptx <- tempfile(fileext = ".pdf")
file <- system.file(package = "doconv",
"doc-examples/example.pptx")
to_pdf(input = file, output = out_pptx)
out_docx <- tempfile(fileext = ".pdf")
file <- system.file(package = "doconv",
"doc-examples/example.docx")
to_pdf(input = file, output = out_docx)
}
