I have been using rst2pdf
and pandoc
to convert the following reStructuredText into PDF:
This is a python example::
def main():
print("Hello World!")
if __name__ == '__main__':
main()
And one for YAML::
server:
name: ts0
location: paris
although the two programs use a different engine to generate PDF (pandoc
even requires you to install TeX for that) the result is a PDF file, from which you cannot correctly cut-and-paste code examples:
The highlighted code (green background) is missing spaces because the empty spaces are generated in these PDF files with offsets.
Copy and paste results in ugly formatted code for many languages, but for programming languages like Occam and Python and for configuration files in YAML, this actually changes the meaning. If you are lucky that actually breaks things and you notice, but the YAML example e.g. is also correct without the spaces at the beginning of the line, it just has a different meaning.
This space-mangling-in-code-examples also happens in the PDF documents generated by the readthedocs site.
If you take the output from:
pandoc example.rst -o example.epub
and view the resulting one page file, you can copy and paste the code correctly. If you convert that example.epub
with calibre (version 2.55.0) via its user-interface you get a PDF file from which you can correctly copy the code (with spaces), but the resulting PDF consists of 3 pages.
Trying to convert from the command line:
ebook-convert example.epub example.pdf
results in a rendering exception, adding the --old-pdf-engine
option the conversion works, but you still have a 3 page PDF instead of a single page.
Converting via HTML:
rst2html example.rst > example.html; ebook-convert example.html example.pdf
pandoc example.rst -o example.html; ebook-convert example.html example.pdf
gives you correclty selectable code:
, but when pasting there are no spaces for the indent, and even the space between Hello
and World
disappears. Apart from that on larger files you get pagebreaks at unwanted places and the layout would need extensive tweaking via style sheets.
I tried conversion via .odt
but that ended up in PDF files with completely unselectable text.
Given the following requirements for the PDF output:
- code fragments correctly selectable with spaces
- results similar in layout to that of
rst2pdf
orreadthedocs
are there any command line tools to generate a PDF file, starting with a .rst
file or some other mark-up format?