Install
openclaw skills install @yon-gjun/code-to-imagesConvert code files to A4-ratio PNG/SVG images with line numbers and syntax highlighting, then merge to PDF. 支持中英文双语说明。
openclaw skills install @yon-gjun/code-to-imagesv2.2.0 — 49 种语言 · 多语言自动识别 · 忠实缩进 · Auto word-wrapping
🇨🇳 中文 | 中文
将源代码文件转换为多页 A4 比例图片(SVG+PNG),然后合并为一个 PDF。
/* */、<!-- -->、--[[ ]]、{- -}、#= =#、(* *)、{ } 等)、三引号字符串(Python/Julia 的 """ ''')、#////--/;/!/' 行注释@ 注解与指令(Java/Python/ObjC/Swift)、$ 变量(Shell/PHP/Perl/PowerShell)、Makefile 目标行与 YAML 键、Markdown 标题;,(){}[] 标点后折行,续行不产生新行号💡
.m文件自动嗅探:含@interface/#import/NSString识别为 Objective-C,否则识别为 MATLAB;Makefile/Dockerfile/CMakeLists.txt按文件名识别。
npm install -g @resvg/resvg-js
pip install img2pdf
gen_code_pdfs.py 顶部的 FILES 列表,填入源文件名FONT_KEY、FONT_SIZE 等配置python gen_code_pdfs.py文件名.pdf + 文件名_images/ 目录(中间 SVG/PNG)| 参数 | 默认值 | 说明 |
|---|---|---|
FONT_KEY | cascadia | 字体:cascadia/firacode/jetbrains/consolas/courier/sourcecode |
FONT_SIZE | 14 | 基础字号 (px),超宽代码自动缩小 |
LINE_HEIGHT_RATIO | 1.65 | 行高系数 |
TAB_WIDTH | 4 | Tab 制表位宽度 |
MIN_FONT_SIZE | 7 | 自动缩小时的最小字号 |
WRAP_IDENT | 2 | 续行额外缩进字符数 |
🇬🇧 English | 中文
Convert source code files into multi-page A4-ratio images (SVG+PNG), then merge into a single PDF.
/* */, <!-- -->, --[[ ]], {- -}, #= =#, (* *), { }…), triple-quoted strings (Python/Julia """ '''), #////--/;/!/' line comments@ annotations & directives (Java/Python/ObjC/Swift), $ variables (Shell/PHP/Perl/PowerShell), Makefile targets & YAML keys, Markdown headings;,(){}[], continuation lines get no new line number💡
.mcontent sniffing: files with@interface/#import/NSStringare treated as Objective-C, otherwise MATLAB;Makefile/Dockerfile/CMakeLists.txtare detected by basename.
npm install -g @resvg/resvg-js
pip install img2pdf
FILES list at the top of gen_code_pdfs.pyFONT_KEY, FONT_SIZE, etc.python gen_code_pdfs.pyfilename.pdf + filename_images/ directory (intermediate SVG/PNG)| Parameter | Default | Description |
|---|---|---|
FONT_KEY | cascadia | Font: cascadia/firacode/jetbrains/consolas/courier/sourcecode |
FONT_SIZE | 14 | Base font size (px), auto-reduces for wide code |
LINE_HEIGHT_RATIO | 1.65 | Line height multiplier |
TAB_WIDTH | 4 | Tab stop width |
MIN_FONT_SIZE | 7 | Minimum font size when auto-reducing |
WRAP_IDENT | 2 | Extra indent for continuation lines |
Save as gen_code_pdfs.py and run with python gen_code_pdfs.py:
#!/usr/bin/env python3
"""Batch convert code files -> A4 images + PDF. Multi-language auto-detection."""
import os, sys, subprocess, json
# ===== CONFIG =====
# Language is auto-detected from each file's extension / basename / content (see LANG / EXT2LANG / BASE2LANG below).
FILES = [/* List source files here, e.g. 'main.c', 'game.py', 'app.ts', 'Makefile' */]
FONT_KEY = '***' # cascadia | firacode | jetbrains | consolas | courier | sourcecode
FONT_SIZE = 14; LINE_HEIGHT_RATIO = 1.65; TAB_WIDTH = 4; MIN_FONT_SIZE = 7; WRAP_IDENT = 2
NODE_EXE = os.environ.get('NODE_EXE', 'node') # full path to node if not on PATH
FONT_NAMES = {'cascadia':'Cascadia Code','firacode':'Fira Code','jetbrains':'JetBrains Mono',
'consolas':'Consolas','courier':'Courier New','sourcecode':'Source Code Pro'}
A4_W, A4_H = 794, 1123; HDR_H = 60; PAD_R = 36; PAD_T = 72; PAD_B = 28
CONTENT_H = A4_H - PAD_T - PAD_B; CHAR_RATIO = 0.60; WRAP_PUNCT = set(';,(){}[]')
CO = {'keyword':'#d63384','register':'#e8590c','macro':'#9c36b5',
'number':'#2b8a3e','string':'#099268','comment':'#868e96','var':'#1971c2','tag':'#0b7285','text':'#212529'}
BG='#ffffff'; HDR_BG='#f1f3f5'; HDR_BD='#dee2e6'; GT_BG='#f8f9fa'; GT_BD='#e9ecef'; LN_COLOR='#868e96'
# Per-language rules (keyword set, comment styles, string quotes, ...).
LANG = {
'python': {'name':'Python','kw':{'def','class','import','from','return','if','for',...},
'sp':set(),'hash':'comment','slash':False,'dash':False,'block':None,'quotes':'"\'','backtick':False,
'triple':('"""',"'''"),'atword':True},
'c': {'name':'C','kw':{'void','char','int','for','if','while',...},'sp':{'P0',...,'P7','RST'},
'hash':'macro','slash':True,'dash':False,'block':('/*','*/'),'quotes':'"\'','backtick':False},
# ... 40+ languages: see gen_code_pdfs.py (LANG / EXT2LANG / BASE2LANG)
}
EXT2LANG = {'.py':'python', '.c':'c', '.h':'c', '.cpp':'c++', '.java':'java', '.js':'js', '.go':'go',
'.rs':'rust', '.rb':'ruby', '.sh':'shell', '.lua':'lua', '.sql':'sql', '.php':'php',
'.cs':'c#', '.css':'css', '.html':'html', '.xml':'html', '.json':'json', '.yaml':'yaml',
'.ts':'typescript', '.kt':'kotlin', '.swift':'swift', '.m':'ambig_m', '.mm':'objective-c',
'.r':'r', '.pl':'perl', '.scala':'scala', '.hs':'haskell', '.jl':'julia', '.zig':'zig',
'.groovy':'groovy', '.ps1':'powershell', '.bat':'batch', '.vb':'vb', '.ino':'arduino',
'.mk':'makefile', '.ini':'ini', '.toml':'ini', '.md':'markdown', '.tex':'tex',
'.cmake':'cmake', '.sol':'solidity', '.erl':'erlang', '.ex':'elixir', '.clj':'clojure',
'.fs':'fsharp', '.pas':'pascal', '.f90':'fortran', '.asm':'assembly'}
BASE2LANG = {'makefile':'makefile','dockerfile':'dockerfile','cmakelists.txt':'cmake'}
# [Full script with LANG/EXT2LANG tables at: gen_code_pdfs.py (local, v2.2.0)]