PDF compiler for your source code
Uses Shiki
for perfect syntax highlighting and shiki-renderer-pdf
to create the PDFs.
npm install pdfc
Most uses are through the CLI, but there is a programatic API.
To compile all files in a directory to PDFs you can use this:
pdfc src
src
is the rootDir
here, so all paths will be relative to that. You cannot include files outside of the rootDir
.
Output is placed in the pdfs
directory by default. To change the output directory you can use the -d
or --out-dir
flag:
pdfc src -d pdf-build
This will compile all files in src
by default. If you want to restrict to a subset of files you can use the --include
flag:
pdfc src --include "src/**/*.js"
This will compile only the js files in src
. The same can be done with the --exclude
flag:
pdfc src --exclude "node_modules" --exclude "src/**/*.test.js"
This will exclude all test files from compilation. Note that node_modules
should also be excluded here because it is only excluded by default if --exclude
is not passed. If include
is specified, exclude
will apply to the included files.
You can also specify a theme to use with the -t
or --theme
flag:
pdfc src -t github-light
If none if specified, it will default to light-plus
. See shiki docs for a list of themes.
Run pdfc --help
for more on what you can do.
Please note that this package is pure ESM, see this for more details.
Compile PDFs, this will also write files to pdfs
by default:
import { compilePdfs } from 'pdfc'
;(async () => {
await compilePdfs({
rootDir: 'src',
include: ['src/**/*.js'],
theme: 'light-plus',
})
})()
Compile string to a PDFDocument
instance:
import fs from 'node:fs/promises'
import { stringToPdf } from 'pdfc'
;(async () => {
const code = 'console.log("Hello World")'
const pdfDocument = await stringToPdf(code, {
lang: 'js',
theme: 'light-plus',
})
const pdfBytes = await pdfDocument.save()
await fs.writeFile('hello-world.pdf', pdfBytes, 'binary')
})()
Read more about what you can do with pdfDocument
here.