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?
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?
Here's a solution that doesn't involve OpenOffice:
sudo apt-get install enscript
enscript my_text_file.txt -o - | ps2pdf - output.pdf
There are more options to
enscript
, e.g.-B
to omit the page header. Seeman enscript
for all of the options.without installing any of the above mentioned software, one can simply do the following with already installed
cupsfilter
:(for options etc. please refer to cupsfilter man pages :-) )
THE SIMPLE ENSCRIPT WAY
Then run enscript on your file with the -P switch and the printer description PDF in this case.
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
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:
this will create a nice looking pdf of test.txt. in the current directory
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.
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:
General and most frequently used syntax for pandoc is:
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:
To install it:
Now you can easily convert any file to pdf.
replace txt with your text format(odt - html - ....)
paps
is a better alternative thanenscript
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:
wkhtmltopdf can handle UTF-8 files (unlike enscript). All problematic chars like ÄÖÜß etc. will be displayed correctly.
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 withsudo apt-get install a2ps
.AFAIK, enscript and a2ps do the same job, but their interface is different and YMMV with each of them.
You can use a2x
To install a2x:
for example:
You can use u2ps too.
This generates both ps and pdf. This accepts UTF-8 text, and supports syntax highlighting through Pango markups.