3

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:

enter image description here

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:

enter image description here

, 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 or readthedocs

are there any command line tools to generate a PDF file, starting with a .rst file or some other mark-up format?

1

You can use the following pipeline

txttopdf.sh:

enscript -B -p- | ps2pdf -dCompressPages=false - -

to generate a PDF which preserves the leading spacing of the source. The special ps2pdf option makes it use the content portion of the intermediate PostScript verbatim, leaving well indented text objects in the PDF. You can verify that by opening it with a plain text editor; the content portion is somewhere towards the end.

However, then it still depends on the PDF viewer whether it discards those leading spaces or not. E.g., evince as well as old acroread do, and they don't let me select the leading whitespace in the viewer. This is the same also if I replace the spaces with non-breaking space (of ISO-8859-1).

Thus, the pipeline does as much as you can do into the PDF file, and then you need to find a viewer that honours the spacing.

  • My evince doesn't have the -B option ( Linux Mint 17.3, i.e. latest). Also the output can be correct (although ugly) and doesn't depend on the reader, as the ebook-convert output shows. – Anthon Apr 30 '16 at 14:08
  • Sorry; it should have been enscript, not evince. Anyhow, I need to check how the ebook PDF file looks, then. – Ralph Rönnquist May 1 '16 at 8:48
  • enscript just does the same as rst2pdf, but without the nice coloring of the code boxes. You can see the indent, but you cannot select them as spaces. – Anthon May 1 '16 at 9:19
  • Yes. I'm trying to figure out which PDF representation the viewer needs in order to allow the selection of leading spaces. How does the ebook output look; does it have a line including "Hello World"? – Ralph Rönnquist May 1 '16 at 9:30
  • Yes of course it includes that line. I updated the question with how the output from ebook-convert looks for the small example, including how evince selects that. – Anthon May 1 '16 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.