Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 將純文字檔轉換為 PDF 兩步驟完成 (enscript + ghostscript)

編輯:關於 Linux

Linux 命令列快速將文字檔轉 PDF,無需安裝圖形化工具。兩步驟:enscript 文字轉 PostScript,ps2pdf (Ghostscript) PostScript 轉 PDF。適合批次處理、列印版面固定、防止誤改。

安裝必要套件

# Debian/Ubuntu/Mint
sudo apt-get install enscript ghostscript

# Fedora/CentOS/RHEL
sudo yum install enscript ghostscript

# Arch Linux
sudo pacman -S enscript ghostscript

轉換步驟

# 1. 文字檔 → PostScript
enscript -p output.ps input.txt

# 2. PostScript → PDF
ps2pdf output.ps output.pdf

# 一行指令完成
enscript -p - input.txt | ps2pdf - output.pdf

常用 enscript 參數

參數說明
-p file輸出 PS 檔案 (用 - 表示 stdout)
-B不印頁首標題
-f font指定字體 (如 Courier10)
-2雙欄排版
-r橫向列印
-G列印頁框
--margins=left:right:top:bottom設定邊距 (單位: 字元/行)

進階:批次轉換目錄下所有 .txt

for f in *.txt; do
    enscript -p - "$f" | ps2pdf - "${f%.txt}.pdf"
done

文字轉 PDF 流程截圖

替代方案

  • pandoc:支援 Markdown、reST、HTML 等多格式轉 PDF (需 LaTeX)。
  • libreoffice --headless --convert-to pdf:轉換 Office 文件。
  • a2ps:類似 enscript,支援語法高亮。
  • 圖形化:Print to File (CUPS PDF 印表機)。
Copyright © Windows教程網 All Rights Reserved