Skip to content

Instantly share code, notes, and snippets.

@sanryuu
Last active May 3, 2019 11:02
Show Gist options
  • Save sanryuu/c9991c2b6240fa45d9a142d28d946c20 to your computer and use it in GitHub Desktop.
Save sanryuu/c9991c2b6240fa45d9a142d28d946c20 to your computer and use it in GitHub Desktop.
A4の日光企画用PDFへの隠しノンブル入れスクリプト。マジックナンバー多くてすみません。
#!/usr/bin/env node
const { execSync } = require("child_process");
const fs = require("fs");
// 環境に合わせて設定を変更する
// B5は WIDTH = 182; HEIGHT = 257;
const PAGE_START = 1;
const WIDTH = 148;
const HEIGHT = 210;
const MARGIN = 30;
execSync("rm -rf tmp");
execSync("mkdir -p tmp");
// バラして
execSync("cpdf -split book.pdf -o tmp/%%%.pdf");
// 隠しノンブル入れて
const files = fs.readdirSync("tmp").sort();
files.forEach((file, i) => {
const page = file.split(".").shift();
const nombre = ((+page + PAGE_START - 1) + "").split("").join("\\n");
const pos = [`${MARGIN/2+2}mm ${HEIGHT /2 + MARGIN/2 +8}mm`, `${WIDTH + MARGIN/2}mm ${HEIGHT /2 + MARGIN/2 +8}mm`][i % 2];
execSync(`cpdf -add-text "${nombre}" -pos-left "${pos}" -font "Courier" -font-size "6" tmp/${file} -o tmp/${page}-nombre.pdf`);
});
// マージし直して
execSync(`cpdf -merge ${files.map(f => f.replace(/(\d{3})/, "tmp/$1-nombre")).join(" ")} -o book_relaese.pdf`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment