Lan*_*nes 55 pdf text-processing imagemagick conversion
我想将.txt
文件转换为.pdf
. 我正在使用这个:
ls | while read ONELINE; do convert -density 400 "$ONELINE" "$(echo "$ONELINE" | sed 's/.txt/.pdf/g')"; done
Run Code Online (Sandbox Code Playgroud)
但这会产生一个“错误”——如果文本文件中有很长的一行,它就不会被换行。
——
此外,如果输出 PDF 可以包含文本,而不是文本图像,那也会很棒。
我有很多很多很多的 TXT 文件。所以不想自己动手。我需要一个自动解决方案,就像我上面提到的那样。
evi*_*oup 49
pandoc可以做到这一点。它更侧重于将标记文本转换为各种格式,但对于简单的纯文本应该没有问题。
pandoc input.txt -o output.pdf
Run Code Online (Sandbox Code Playgroud)
Kei*_*ith 25
一种方法是使用 CUPS 和 PDF 伪打印机将文本“打印”为 PDF 文件。
另一种是使用enscript编码为 postscript,然后使用 ghostscript 包中的 ps2pdf 文件从 postscript 转换为 PDF。
Gon*_*iro 25
您可以使用 Vim 将文本打印到 PostScript 文件,然后将其转换为 PDF,只要 Vim 是使用该+postscript
功能编译的。
为此,您可以使用该:hardcopy > {filename}
命令。例如,您可以打开example.txt
并执行
:hardcopy > example.ps
Run Code Online (Sandbox Code Playgroud)
这将产生一个example.ps
包含所有文本的文件example.txt
。PostScript 文件中每一页的页眉将包含原始文件名和页码。
然后您可以使用以下命令将 PostScript 文件转换为 PDF
ps2pdf example.ps
Run Code Online (Sandbox Code Playgroud)
这将创建example.pdf
.
您可以使用以下命令直接从终端执行相同操作(无需与 Vim 交互)
vim example.txt -c "hardcopy > example.ps | q"; ps2pdf example.ps
Run Code Online (Sandbox Code Playgroud)
这将example.txt
在 Vim 中打开并执行传递给-c
选项的命令,在本例中是一个hardcopy
命令,后跟一个退出 ( q
) 命令。然后执行ps2pdf
以生成最终文件。
有关更多选项,请参阅带有:help :hardcopy
.
Cal*_*leb 21
LibreOffice / OpenOffice 以及大多数其他文字处理器 (Abiword) 可以很容易地做到这一点。
有一个名为的小实用程序unoconv
,它使用 LibreOffice 代码库在命令行上进行文件格式转换。它可以读取和写入 LibreOffice 可以读取和写入的任何格式组合,并且可以非常轻松地执行诸如在命令行上doc
进行pdf
转换之类的操作。简单txt
到pdf
会很容易。
cei*_*art 10
只需使用免费且开源的text2pdf。在该链接中,您可以下载 windows、solaris、dos 的源代码或预编译二进制文件。
我可以毫无问题地将它用于 AIX OS。编译非常简单,只需将 text2pdf.c 和 Makefile 保存到同一目录并键入make
. (这里我在 AIX 上设置了变量 CC=gcc,在 linux 上这不是问题)
$ ./text2pdf -h
text2pdf [options] [filename]
text2pdf makes a 7-bit clean PDF file (version 1.1) from any input file.
It reads from standard input or a named file, and writes the PDF file
to standard output.
There are various options as follows:
-h show this message
-f<font> use PostScript <font> (must be in standard 14, default: Courier)
-I use ISOLatin1Encoding
-s<size> use font at given pointsize (default 10)
-v<dist> use given line spacing (default 12 points)
-l<lines> lines per page (default 60, determined automatically
if unspecified)
-c<chars> maximum characters per line (default 80)
-t<spaces> spaces per tab character (default 8)
-F ignore formfeed characters (^L)
-A4 use A4 paper (default Letter)
-A3 use A3 paper (default Letter)
-x<width> independent paper width in points
-y<height> independent paper height in points
-2 format in 2 columns
-L landscape mode
Note that where one variable is implied by two options, the second option
takes precedence for that variable. (e.g. -A4 -y500)
In landscape mode, page width and height are simply swapped over before
formatting, no matter how or when they were defined.
text2pdf v1.1 (c) Phil Smith, 1996
$ ./text2pdf -f"Courier" -s6 -c216 -v6 -L -A4 ./rep3.txt >rep3.pdf
Run Code Online (Sandbox Code Playgroud)
LibreOffice 为此而工作。用法:
libreoffice --convert-to "pdf" file.txt
输出将被称为file.pdf
。
小智 7
使用 enscript 创建一个 .ps 文件,然后 ps2pdf(或 ps2pdfwr)转换为 .pdf
以下脚本创建一个 .pdf 文件,左右边距为 10 pt,并使用 7.3 pts 宽和 10 pts 高的 courier 字体,因此 132 列打印输出适合 8 1/2 X 11 页面。使用 enscript 设置页面、字体等。
$ enscript -B --margins=10:10: -o outputfile.ps -f Courier@7.3/10 inputfile
$ ps2pdfwr outputfile.ps newfile.pdf
$ rm outputfile.ps
Run Code Online (Sandbox Code Playgroud)