我有一个字符串,其中的文本以我希望的方式显示。这是一个ascii艺术项目。我想将此文本转换为 pdf,以便我可以在当地的印刷店打印带有文本的海报。
字体需要固定宽度,并且没有标签或类似的东西。行以 '\n' 字符结尾。有几个空格字符。
图像宽数百字,高数百字。因此,它不适合典型页面的空间。
我如何在python中做到这一点?
尝试模块pyPdf,可能会为您解决问题!
或pyfPdf,允许您在创建新 PDF 文件时设置字体(setFont 示例)
from fpdf import FPDF
pdf=FPDF()
pdf.add_page()
pdf.set_font('Courier','B',16)
pdf.cell(40,10,'Hello World!')
pdf.output('tuto1.pdf','F')
There are several more methods which could create a printable file from your ASCII art.
To demonstrate the method...
...first, create a text file named my.mvg
and put the following content in:
text 8,8 "
_ ____ ____ ___ ___ _ _
/ \ / ___| / ___|_ _|_ _| / \ _ __| |_
/ _ \ \___ \| | | | | |_____ / _ \ | '__| __|
/ ___ \ ___) | |___ | | | |_____/ ___ \| | | |_
/_/ \_\____/ \____|___|___| /_/ \_\_| \__|
"
...next, run one of these commands:
convert \
-font "Courier" \
-size 800x200 xc:none \
-box yellow \
-pointsize 12 \
-gravity center \
-draw @my.mvg \
my-ascii-art.pdf
to get a PDF as output file, or
convert \
-font "Liberation-Mono-Regular" \
-size 800x200 xc:none \
-box orange \
-pointsize 14 \
-gravity center \
-draw @my.mvg \
my-ascii-art.png
for PNG output. Of course for each of the two commands you can play with -size
and -box
and -pointsize
to your heart's contents...
You may need to remove the \n
character from your line ends, though...
Note the ImageMagick method will create raster graphics, even if the output is PDF (and probably rather large files).**
a2ps
+ Ghostscript:To demonstrate this method...
...first, create a text file named henryb.txt
with this content:
_
|_| _ ._ ._ |_)
| |(/_| || \/ |_)o
/
...next, run this command to let a2ps
and Ghostscript cooperate to create a PDF file:
a2ps \
--output=- \
--columns=1 \
--borders=0 \
--no-header \
--landscape \
--medium=a5 \
henryb.txt \
| \
gs \
-o henryb.pdf \
-sDEVICE=pdfwrite \
-g1000x2500 \
-dAutoRotatePages=/None \
-
or, for PNG output:
a2ps \
--output=- \
--columns=1 \
--borders=0 \
--no-header \
--landscape \
--medium=a5 \
henryb.txt \
| \
gs \
-o henryb.png \
-sDEVICE=pngalpha \
-g100x250 \
-
(-gNNNxMMM
gives the output dimensions in pixels. Ghostscript by default uses 720 pixels per inch for PDF distance calculations, but 72 ppi for raster image calculations, unless overridden with the -rNNN
resolution setting...)
enscript
+ Ghostscript:To demonstrate this method...
...first, create a text file named henryb-2.txt
with this content:
m m mmmmm
# # mmm m mm m mm m m # #
#mmmm# #" # #" # #" " "m m" #mmmm"
# # #"""" # # # #m# # #
# # "#mm" # # # "# #mmmm" #
m"
""
...next, run this command to let enscript
and Ghostscript cooperate to create a PDF file:
enscript \
--output=- \
--columns=1 \
--no-header \
--landscape \
--media=A5 \
henryb-2.txt \
| \
gs \
-o henryb-2.pdf \
-sDEVICE=pdfwrite \
-g1500x4000 \
-
or, for PNG output:
enscript \
--output=- \
--columns=1 \
--no-header \
--landscape \
--media=A5 \
henryb-2.txt \
| \
gs \
-o henryb-2.png \
-sDEVICE=pngalpha \
-g150x400 \
-
试试报告实验室 它有一些很酷的功能,比如