How to convert TXT to PDF?

pandoc can do this. It's more focused on converting marked-up text to various formats, but it should have no problems with simple plaintext.

pandoc input.txt -o output.pdf

One method is to use CUPS and the PDF psuedo-printer to "print" the text to a PDF file.

Another is to use enscript to encode to postscript and then convert from postscript to PDF using the ps2pdf file from ghostscript package.


You can print text to a PostScript file using Vim and then convert it to a PDF, as long as Vim was compiled with the +postscript feature.

For this you use the :hardcopy > {filename} command. For example you can open example.txt and execute

:hardcopy > example.ps

which will produce a file example.ps containing all the text in example.txt. The header of each page in the PostScript file will contain the original filename and the page number.

Then you can convert the PostScript file into a PDF by using the following command

ps2pdf example.ps

which will create example.pdf.

You can do the same directly from a terminal (without interacting with Vim) by using the following command

vim example.txt -c "hardcopy > example.ps | q"; ps2pdf example.ps

This opens example.txt in Vim and executes the command passed to the -c option, which in this case is a hardcopy command followed by a quit (q) command. Then it executes ps2pdf to produce the final file.

For more options see the help files with :help :hardcopy.