Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Create educational and knowledge comics with multiple art styles (manga, ligne-claire, ink-brush) and tone combinations.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/merge-to-pdf.ts
1import { existsSync, readdirSync, readFileSync } from "fs";2import { join, basename } from "path";3import { PDFDocument } from "pdf-lib";45interface PageInfo {6filename: string;7path: string;8index: number;9promptPath?: string;10}1112function parseArgs(): { dir: string; output?: string } {13const args = process.argv.slice(2);14let dir = "";15let output: string | undefined;1617for (let i = 0; i < args.length; i++) {18if (args[i] === "--output" || args[i] === "-o") {19output = args[++i];20} else if (!args[i].startsWith("-")) {21dir = args[i];22}23}2425if (!dir) {26console.error("Usage: bun merge-to-pdf.ts <comic-dir> [--output filename.pdf]");27process.exit(1);28}2930return { dir, output };31}3233function findComicPages(dir: string): PageInfo[] {34if (!existsSync(dir)) {35console.error(`Directory not found: ${dir}`);36process.exit(1);37}3839const files = readdirSync(dir);40const pagePattern = /^(\d+)-(cover|page)(-[\w-]+)?\.(png|jpg|jpeg)$/i;41const promptsDir = join(dir, "prompts");42const hasPrompts = existsSync(promptsDir);4344const pages: PageInfo[] = files45.filter((f) => pagePattern.test(f))46.map((f) => {47const match = f.match(pagePattern);48const baseName = f.replace(/\.(png|jpg|jpeg)$/i, "");49const promptPath = hasPrompts ? join(promptsDir, `${baseName}.md`) : undefined;5051return {52filename: f,53path: join(dir, f),54index: parseInt(match![1], 10),55promptPath: promptPath && existsSync(promptPath) ? promptPath : undefined,56};57})58.sort((a, b) => a.index - b.index);5960if (pages.length === 0) {61console.error(`No comic pages found in: ${dir}`);62console.error("Expected format: 00-cover-slug.png, 01-page-slug.png, etc.");63process.exit(1);64}6566return pages;67}6869async function createPdf(pages: PageInfo[], outputPath: string) {70const pdfDoc = await PDFDocument.create();71pdfDoc.setAuthor("baoyu-comic");72pdfDoc.setSubject("Generated Comic");7374for (const page of pages) {75const imageData = readFileSync(page.path);76const ext = page.filename.toLowerCase();77const image = ext.endsWith(".png")78? await pdfDoc.embedPng(imageData)79: await pdfDoc.embedJpg(imageData);8081const { width, height } = image;82const pdfPage = pdfDoc.addPage([width, height]);8384pdfPage.drawImage(image, {85x: 0,86y: 0,87width,88height,89});9091console.log(`Added: ${page.filename}${page.promptPath ? " (prompt available)" : ""}`);92}9394const pdfBytes = await pdfDoc.save();95await Bun.write(outputPath, pdfBytes);9697console.log(`\nCreated: ${outputPath}`);98console.log(`Total pages: ${pages.length}`);99}100101async function main() {102const { dir, output } = parseArgs();103const pages = findComicPages(dir);104105const dirName = basename(dir) === "comic" ? basename(join(dir, "..")) : basename(dir);106const outputPath = output || join(dir, `${dirName}.pdf`);107108console.log(`Found ${pages.length} pages in: ${dir}\n`);109110await createPdf(pages, outputPath);111}112113main().catch((err) => {114console.error("Error:", err.message);115process.exit(1);116});117