-
-
Save MadcapJake/0bbe0ad22e21d9175e5d4075d7606786 to your computer and use it in GitHub Desktop.
convert powerpoint pdf svgs into pngs for use on courses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// mkdir input && mkdir cleaned && mkdir raster | |
// pdf2svg Logography\ for\ Kokanu.pdf input/kokanu_%d.svg all | |
// node kokanu.mjs pre | |
// node kokanu.mjs post | |
import fs from 'fs/promises' | |
import fsx from 'fs' | |
import readline from 'readline' | |
import { spawn } from 'child_process' | |
const inputDirectory = 'input' | |
const cleanedDirectory = 'cleaned' | |
const rasterDirectory = 'raster' | |
async function removeFluff(inFileName, outFileName) | |
{ | |
const fileStream = fsx.createReadStream(`${inputDirectory}/${inFileName}.svg`) | |
const lineReader = readline.createInterface( | |
{ | |
input: fileStream, | |
crlfDelay: Infinity | |
}) | |
const lines = [] | |
for await (const line of lineReader) | |
{ | |
lines.push(line) | |
} | |
const cleaned = lines | |
.filter(line => | |
line.startsWith("<?xml") | |
|| line.startsWith("<svg") | |
|| line.startsWith("</svg>") | |
|| (line.startsWith("<path") | |
&& line.includes("stroke-width"))) | |
.join("\n") | |
await fs.writeFile(`${cleanedDirectory}/${outFileName}.svg`, cleaned) | |
} | |
async function finalize(fileName) | |
{ | |
const convertToPNG = spawn('convert', [ | |
'-background', 'none', | |
'+level-colors', 'transparent,black', | |
'-gravity', 'center', | |
'-extent', '400x400', | |
'-format', 'png', | |
`${cleanedDirectory}/${fileName}.svg`, | |
`${rasterDirectory}/${fileName}.png`]) | |
await new Promise((resolve, reject) => { | |
convertToPNG.on('close', resolve); | |
}); | |
const trimPNG = spawn('mogrify', [ | |
'-trim', | |
`${rasterDirectory}/${fileName}.png`]) | |
await new Promise((resolve, reject) => { | |
trimPNG.on('close', resolve); | |
}); | |
const resizePNG = spawn('mogrify', [ | |
'-background', 'transparent', | |
'-resize', '400x400', | |
'-gravity', 'center', | |
'-extent', '400x400', | |
`${rasterDirectory}/${fileName}.png`]) | |
await new Promise((resolve, reject) => { | |
resizePNG.on('close', resolve); | |
}); | |
} | |
let end = false | |
async function main() | |
{ | |
var args = process.argv.slice(2); | |
const fileStream = fsx.createReadStream(`words.txt`) | |
const lineReader = readline.createInterface( | |
{ | |
input: fileStream, | |
crlfDelay: Infinity | |
}) | |
const words = [] | |
for await (const line of lineReader) | |
{ | |
words.push(line) | |
} | |
await words.forEach(async (word, i) => | |
{ | |
if (args[0] == 'pre') | |
{ | |
await removeFluff(`kokanu_${i+3}`, word) | |
} | |
else if (args[0] == 'post') | |
{ | |
await finalize(word) | |
} else | |
{ | |
throw new Error("invalid subcommand") | |
} | |
}) | |
end = true | |
} | |
main() | |
function wait() | |
{ | |
if (!end) setTimeout(wait, 1000) | |
} | |
wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I then use this to fix the ones that have been filled in incorrectly: