
Convert PDFs to DOCX Using the Best Available Backend
Source:R/pdf_to_docx.R
convert_pdfs_to_docx.RdBackend preference is python (pdf2docx) -> LibreOffice -> Word COM
(Windows only). When the python backend fails on an individual file and
LibreOffice is present, that file is retried once with LibreOffice.
Arguments
- conversions
A data frame with character columns
srcanddest, and optionallytemp_dest, a second location each converted file is copied to.- backend
One of
"auto","python","libreoffice","word_com".- strict
If
TRUE(default), stop at the first bad row and, when every row is fine, error with an actionable message if no backend is available. SetFALSEto warn and skip instead; a skipped row comes back withconverted = FALSE.- timeout
Seconds a converter may take on one file before it is stopped; it applies to every backend (python, LibreOffice and Word COM). A file that times out gets a warning that says so and comes back with
converted = FALSE(the python backend then retries it once with LibreOffice, which gets its owntimeout). Stopping a converter stops the process that was started; any process that one started in turn (LibreOffice'ssoffice.bin, or the Word instance behind Word COM) may keep running and has to be closed by hand.
Details
The rows of conversions are checked before any backend is looked for,
so a bad row is reported the same way on every machine, whether or not a
converter is installed. A row is bad when its src does not exist or
is empty (0 bytes), when its dest is missing, or when its dest
or temp_dest ends in .pdf (a DOCX is not a PDF, and the file
that is there would be overwritten). A src that resolves to the same
file as its dest or temp_dest, however the path is spelled
(./a.pdf, another case), and a dest that is the same file as
its temp_dest, are always errors, whatever strict is, because
converting would overwrite the PDF or empty the copy. Rows that pass are
then converted; if none pass, no backend is looked for at all.
A converter writes into a fresh file next to dest, and that file
replaces dest only when the conversion succeeded and left a non-empty
file. For pdf2docx and Word COM, succeeded means that the converter exited
with status 0. LibreOffice's exit status is not looked at: it writes into a
private, empty folder, so whatever it leaves there was written by this run,
and a first start with a fresh profile may exit non-zero after a good
conversion. A time-out is a failure for every backend. A DOCX that was
already at dest (from an earlier run, say) is never mistaken for the
result: when the conversion fails, dest is left exactly as it was (an
old file is neither deleted nor reported as converted, and a half-written
file from a crashed converter is discarded), and the row comes back with
converted = FALSE.