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.0.5 — Brace-depth indentation · Auto word-wrapping
🇨🇳 中文 | 中文
将源代码文件转换为多页 A4 比例图片(SVG+PNG),然后合并为一个 PDF。
{} 嵌套层级决定缩进,每级 4 字符宽度;,(){}[] 标点后折行,续行不产生新行号/* */ 跨界注释完整处理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.
{} nesting, each level = 4 char widths;,(){}[], continuation lines get no new line number/* */ cross-line supportnpm 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. One PDF per source file."""
import os, sys, subprocess, json
# ===== CONFIG =====
FILES = [/* List source files here */]
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
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(';,(){}[]')
KW = {'void','char','int','u8','u16','u32','uchar','unsigned','for','if','else','while',
'switch','case','break','return','static','bit','sbit','xdata','idata','code','interrupt',
'do','default','continue','struct','typedef','enum','const','#ifndef','#define','#endif','#ifdef','extern'}
RG = {'P0','P1','P2','P3','P4','P5','P6','P7','RST','SCLK','IO','SCK'}
CO = {'keyword':'#d63384','register':'#e8590c','macro':'#099268',
'number':'#2b8a3e','string':'#099268','comment':'#868e96','var':'#1971c2','text':'#212529'}
BG='#ffffff'; HDR_BG='#f1f3f5'; HDR_BD='#dee2e6'; GT_BG='#f8f9fa'; GT_BD='#e9ecef'; LN_COLOR='#868e96'
# [Full script at: https://github.com/yon-gjun/code-to-images/blob/master/gen_code_pdfs.py]
# Run the script directly from the repo for the complete, tested version.