Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

7 Answers 7

up vote 18 down vote accepted

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.

share|improve this answer
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 at 12:20

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

Hope this helps

share|improve this answer
    
Not what OP asked for. –  enzotib Feb 19 '11 at 12:39
2  
? OP? who is OP and why vote this down when it answers the question? –  RolandiXor Feb 19 '11 at 13:01

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

share|improve this answer

paps is a better alternative than enscript

paps file.txt | ps2pdf - output.pdf
share|improve this answer
3  
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 1 '13 at 6:15

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.

share|improve this answer

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
share|improve this answer

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 - ....)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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