Pandoc does not seem to include .rtf files. Can I add them

pandoc

I am using Ubuntu 22.04 LTS

I wanted to convert .rtf files to PDFs using Python.

I found this on stackoverflow:

import pypandoc

def convert_rtf_to_pdf(input_rtf, output_pdf):
    pypandoc.convert_file(input_rtf, 'pdf', outputfile=output_pdf)

if __name__ == "__main__":
    input_rtf_path = "Path to your RTF File"
    output_pdf_path = "path to your pdf file output with name /output.pdf"

    convert_rtf_to_pdf(input_rtf_path, output_pdf_path)

But I get this error:

RuntimeError: Invalid input format! Got "rtf" but expected one of
these: commonmark, creole, csv, docbook, docx, dokuwiki, epub, fb2,
gfm, haddock, html, ipynb, jats, jira, json, latex, man, markdown,
markdown_github, markdown_mmd, markdown_phpextra, markdown_strict,
mediawiki, muse, native, odt, opml, org, rst, t2t, textile, tikiwiki,
twiki, vimwiki

Can I somehow add .rtf files to Ubuntu pandoc, then convert them to PDF?

Looking further, I found this, which shows rtf is available:

print(pypandoc.get_pandoc_formats())

(['commonmark', 'creole', 'csv', 'docbook', 'docx', 'dokuwiki',
'epub', 'fb2', 'gfm', 'haddock', 'html', 'ipynb', 'jats', 'jira',
'json', 'latex', 'man', 'markdown', 'markdown_github', 'markdown_mmd',
'markdown_phpextra', 'markdown_strict', 'mediawiki', 'muse', 'native',
'odt', 'opml', 'org', 'rst', 't2t', 'textile', 'tikiwiki', 'twiki',
'vimwiki'], ['asciidoc', 'asciidoctor', 'beamer', 'commonmark',
'context', 'docbook', 'docbook4', 'docbook5', 'docx', 'dokuwiki',
'dzslides', 'epub', 'epub2', 'epub3', 'fb2', 'gfm', 'haddock', 'html',
'html4', 'html5', 'icml', 'ipynb', 'jats', 'jats_archiving',
'jats_articleauthoring', 'jats_publishing', 'jira', 'json', 'latex',
'man', 'markdown', 'markdown_github', 'markdown_mmd',
'markdown_phpextra', 'markdown_strict', 'mediawiki', 'ms', 'muse',
'native', 'odt', 'opendocument', 'opml', 'org', 'pdf', 'plain',
'pptx', 'revealjs', 'rst', 'rtf', 's5', 'slideous', 'slidy', 'tei',
'texinfo', 'textile', 'xwiki', 'zimwiki'])

Best Answer

Your get_pandoc_formats() output contains two lists, only one of which has rtf in it. I suspect that rtf in this case is only supported as an output file format (since it is only in the second list), and not for input (which I'd guess is the first list). You can confirm this by running:

pandoc --list-input-formats | grep rtf
pandoc --list-output-formats | grep rtf

The commit adding a reader (input format support) for RTF is available in versions 2.14.2 and newer, but Ubuntu 22.04 only has 2.9.2. You can try using the binaries provided by Pandoc at https://github.com/jgm/pandoc/releases/latest (linked to from the Pandoc website), as Pandoc is a Haskell program, the final binary should be relatively independent of the base distro version.