自己学编程时做了很多笔记,如今累积起来已经有几十个了,有很多图片链,怕哪天图床垮了图片就找不到了,于是就想把当时的 md 文件都转成 pdf ,看了点文章,就开始实践:
1.下载 pandoc 和 CTex 2.chatGpt 弄一个脚本,需求是扫描脚本所在目录所有的 md 文件,并输出 pdf 文件到指定目录下,pdf 引擎用 xelatex, 转换用的 latex 模版需要和 md 文件目录放在一起,由于我用的 typora ,想让输出的界面效果和 typora 的尽量一致,于是在 typora 官网上找到了个叫eisvogel的模版
脚本如下:
import os
import subprocess
import sys
directory_path = './'
# 确定输出文件夹
if len(sys.argv) == 2:
output_folder = sys.argv[1]
else:
output_folder = ""
# 确保输出文件夹存在
if output_folder and not os.path.exists(output_folder):
os.makedirs(output_folder)
# 获取目录下所有的 md 文件
md_files = [f for f in os.listdir(directory_path) if f.endswith(".md")]
for md_file in md_files:
# 构建输出文件路径
if output_folder:
output_path = f'{output_folder}/{md_file.replace(".md", ".pdf")}'
else:
output_path = f'{md_file.replace(".md", ".pdf")}'
# 构建 pandoc 命令
cmd = f'pandoc "{md_file}" -o "{output_path}" --pdf-engine=xelatex --template eisvogel.latex --listings -V "CJKmainfont=‘Microsoft YaHei’"'
print(cmd)
os.system(cmd)
3.执行脚本,结果每个 md 文件都报一堆看不懂的输出然后报错 Package fontspec Error ,但是我检查过系统是有 Microsoft YaHei 这个字体的。
pandoc "C++ - 多重继承.md" -o "./pdf/C++ - 多重继承.pdf" --pdf-engine=xelatex --listings -V "CJKmainfont=‘Microsoft YaHei’"
This is METAFONT, Version 2.71828182 (MiKTeX 24.4)
(D:\CTEX\MiKTeX\fonts/source/public/othello\OT.mf
>> (0,0,line_thickness,0,0,line_thickness)
! Transform components aren't all known.
<to be read again>
;
set_black_circle->...circle.scaled.line_thickness;
lft.x1=1/22size;y1=1/2size...
l.39 set_black_circle
;
>> 0.04347x2+0.52174
! Undefined x coordinate has been replaced by 0.
<to be read again>
..
set_black_circle->...size;y2=1/2size;filldraw.z1..
z2..cycle;black_circle:=cu...
l.39 set_black_circle
;
>> 0.47826x2+0.23914
! Undefined y coordinate has been replaced by 0.
<to be read again>
..
set_black_circle->...size;y2=1/2size;filldraw.z1..
z2..cycle;black_circle:=cu...
l.39 set_black_circle
;
>> x2
! Undefined x coordinate has been replaced by 0.
<to be read again>
..
set_black_circle->...;y2=1/2size;filldraw.z1..z2..
cycle;black_circle:=curren...
l.39 set_black_circle
;
>> 0.47826x2+0.23914
! Undefined y coordinate has been replaced by 0.
<to be read again>
..
set_black_circle->...;y2=1/2size;filldraw.z1..z2..
cycle;black_circle:=curren...
l.39 set_black_circle
;
。。。。
<to be read again>
..
l.647 draw z5..
z6;
>> -0.5border_line_thickness
! Undefined y coordinate has been replaced by 0.
<to be read again>
;
l.647 draw z5..z6;
[18] )
(see the transcript file for additional information)
Font metrics written on OT.tfm.
Output written on OT.600gf (19 characters, 504 bytes).
Transcript written on OT.log.
Sorry, but miktex-maketfm did not succeed.
The log file hopefully contains the information to get MiKTeX going again:
D:\CTEX\UserData\miktex\log\miktex-maketfm.log
Error producing PDF.
! Package fontspec Error:
(fontspec) The font "鈥楳 icrosoft YaHei 鈥?pandoc: <stderr>: hPutChar: invalid argument (Invalid argument)
奇怪的是,把脚本输出的 cmd 字符串直接在 Powershell 上运行就能正常输出,于是我又把脚本改成只输出 cmd 文本,然后 CV 这些文本到 powershell 上批量运行,结果同样也有上述的错误,也就是说,可以单处理,就是不能批处理。
个人对 latex 的处理方式毫无经验,希望有懂的网友解释一下。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.