Converts a .docx file to PDF with LibreOffice, or with Microsoft Word
through the doconv package. LibreOffice is free and runs on all
platforms. Each call uses a fresh temporary LibreOffice profile, so it
works while a LibreOffice window is open and in parallel runs.
Usage
docx_to_pdf(
docx,
pdf = NULL,
engine = c("auto", "libreoffice", "word"),
overwrite = FALSE,
verbose = TRUE,
fonts = NULL
)Arguments
- docx
Path to an existing
.docxfile.Path of the PDF to write.
NULL(default) uses the path ofdocxwith the extension changed to.pdf.- engine
"auto"(default) tries LibreOffice, then Word throughdoconvif that package is installed."libreoffice"and"word"force one engine.- overwrite
Overwrite an existing
pdf? DefaultFALSE.- verbose
Print progress messages? Default
TRUE.- fonts
Font files (
.ttf,.otf,.ttc) or folders of font files that LibreOffice should use for this conversion, in addition to the fonts it finds itself. Use it when a script shows as blank text or boxes in the PDF although the font is on the machine.NULL(default) uses the optionconvertxlsform.fonts, if set. Ignored by the Word engine.
Details
If LibreOffice is installed in an unusual place, set the option
convertxlsform.soffice or the environment variable
CONVERTXLSFORM_SOFFICE to the path of the soffice binary.
Examples
if (interactive() && has_libreoffice()) {
docx <- tempfile(fileext = ".docx")
doc <- officer::read_docx()
doc <- officer::body_add_par(doc, "Hello")
print(doc, target = docx)
pdf <- docx_to_pdf(docx, pdf = tempfile(fileext = ".pdf"))
file.exists(pdf)
}
