45

In gedit, printing a file gives an option of printing to file where the file can be saved as a pdf. How can I do the same thing from the command line?

skypemesm
  • 665

11 Answers11

50

Here's a solution that doesn't involve OpenOffice:

  1. sudo apt-get install enscript

  2. enscript my_text_file.txt -o - | ps2pdf - output.pdf

There are more options to enscript, e.g. -B to omit the page header. See man enscript for all of the options.

  • 1
    Good alternative Stefano especially for anyone not using open/libreoffice as unoconv is dependent on either of these being installed. – Allan Feb 19 '11 at 13:22
  • +1 for the nice answer, the one I was about to give, I just decided to give one I think will add something to what is already here. – Sabacon Feb 19 '11 at 13:43
  • Isn't there any inbuilt bash programme or command to do this? – kashminder Jun 28 '14 at 12:20
  • 1
    Thanks! It works for most texts, however enscript only works with old encoding, it doesn't work with UTF-8 for instance so you can't convert a text if it includes the euro symbol (€) for instance... – Antoine Dusséaux Feb 22 '17 at 14:46
33

without installing any of the above mentioned software, one can simply do the following with already installed cupsfilter:

cupsfilter foo.txt > foo.pdf

(for options etc. please refer to cupsfilter man pages :-) )

David Foerster
  • 36,264
  • 56
  • 94
  • 147
Abel Tom
  • 641
14

THE SIMPLE ENSCRIPT WAY

sudo apt-get install cups-pdf enscript

Then run enscript on your file with the -P switch and the printer description PDF in this case.

enscript -B -PPDF test.txt

A printer with description PDF was created when you installed cups-pdf, when you use enscript with that printer your document will be sent to the PDF printer and will be printed to file, created as .pdf in your /home/PDF directory, the command above will print the text file test.txt as a pdf in the PDF directory.

USING UNOCONV

sudo apt-get install unoconv

You can use unoconv in standalone mode, this means that in absence of an OpenOffice listener, it will start its own:

In the directory where your file is located run:

unoconv -f pdf test.txt

this will create a nice looking pdf of test.txt. in the current directory

Sabacon
  • 40,058
7

There is a command line program that can convert between all of the formats supported open/libre office called unoconv so if you need to batch convert with a script it will come in handy.

sudo apt-get install unoconv
Zanna
  • 70,465
Allan
  • 11,582
7

Pandoc is a must have swiss knife tool when it comes to conversion between various markup languages. To get you started first install pandoc converter:

sudo apt-get install pandoc

General and most frequently used syntax for pandoc is:

pandoc -f <from format> -t <to format> <source file>

Please note that you need texlive-latex-base package to be installed first, before you can convert to PDF format. Otherwise you will get a following error:

pandoc: pdflatex not found. pdflatex is needed for pdf output.

To install it:

sudo apt-get install texlive-latex-base

Now you can easily convert any file to pdf.

pandoc -t txt -t pdf source.txt

replace txt with your text format(odt - html - ....)

Maythux
  • 84,289
  • 2
    Compared to the other answers, pandoc plus texlive produces bad output (not respecting newlines), and is overkill and confusing to boot. Lots more packages are needed, pulling in texlive, texlive-fonts-recommended, ruby, etc. E.g. I get pandoc: Unknown writer: pdf until I install texlive-latex-base. This is on trusty. See https://github.com/jgm/pandoc/issues/1155 Then I got an error about a missing font, then one about url.sty, etc. – nealmcb Mar 14 '15 at 03:54
  • Did you mean for the first -t in pandoc -t txt -t pdf source.txt to be -f? – Keith Bennett Dec 16 '20 at 01:09
5

paps is a better alternative than enscript

paps file.txt | ps2pdf - output.pdf
hwc
  • 51
  • 1
  • 1
  • 10
    Welcome to Ask Ubuntu hwc! While your post does answer the question, it's always better to include more information. For example, why is paps a better alternative? And if paps is not installed by default, what is the command to install it? You can always [edit] your answer by clicking on edit right underneath the body of the answer. Thanks! – Alaa Ali Sep 01 '13 at 06:15
4

I had some issues with german Umlaute (ÄÖÜ) with the above mentioned solutions. Or, in the case of paps, the resulting PDF does not have a text layer.

The best solution for me was to use wkthmltopdf. It seems to be not documented, but you can easily convert text files into PDF files by using this syntax:

wktmltopdf <textfile> <pdffile>

wkhtmltopdf can handle UTF-8 files (unlike enscript). All problematic chars like ÄÖÜß etc. will be displayed correctly.

  • This has been fixed in the latest version of paps, which is able to output selectable pdf. See: https://github.com/dov/paps . – Dov Grobgeld Jan 15 '18 at 09:03
3

As posted above, enscript is a popular way to convert text to postscript, which can then be further converted to PDF.
A similar tool, which has been around for a long time, is a2ps. It has a large number of options, including putting multiple pages on a physical page. Install ap2s with sudo apt-get install a2ps.

AFAIK, enscript and a2ps do the same job, but their interface is different and YMMV with each of them.

dirkjot
  • 133
  • An example of using a2ps would help. The --delegate option seems to be needed for pdf, but the man page doesn't really help there. – nealmcb Mar 14 '15 at 03:56
2

You can use a2x

a2x - convert Asciidoc text file to PDF, XHTML, HTML Help, ODF, manpage or plain text

To install a2x:

sudo apt-get install asciidoc

for example:

 a2x -f pdf testfile.txt
Maythux
  • 84,289
1

You can use u2ps too.

This generates both ps and pdf. This accepts UTF-8 text, and supports syntax highlighting through Pango markups.

0

Here is a modern alternative that does not require installing unoconv:

libreoffice --headless --convert-to pdf myfile.txt

This will create a file in the current directory just like unoconv. Since LibreOffice is installed by default on Ubuntu, this solution has the benefit of not requiring additional software.

Daniel T
  • 4,594