← All Posts

How to Do PDF Generation on Claude Code

Claude Code can generate PDFs by writing and running Node.js or Python scripts that use libraries like Puppeteer, pdfkit, or html-pdf-node. You prompt Claude Code in plain English, it scaffolds the code, installs dependencies, and executes the output directly in your terminal. No GUI, no context switching. Works best for developers automating reports, invoices, or documentation exports from existing data sources.

  • Puppeteer (headless Chrome) produces pixel-perfect HTML-to-PDF results
  • pdfkit is lightweight and ideal for programmatic layouts
  • Claude Code can scaffold, install, and run the full pipeline in one session

What is PDF generation in Claude Code?

Claude Code is a terminal-based AI coding agent that operates directly in your shell. When you ask it to "generate a PDF from this HTML file" or "create an invoice PDF from this JSON data," it writes the necessary script, installs any missing packages via npm install or pip install, runs it, and shows you the result inline. No separate IDE or browser tab required.

This makes it well-suited for one-off PDF tasks, recurring report automation, or wiring PDF generation into a CI pipeline without leaving your terminal flow.

How to generate a PDF using Puppeteer in Claude Code

Puppeteer renders a headless Chromium browser and saves any URL or HTML string as a PDF. It produces output that matches how a browser would print the page, including CSS, web fonts, and layout.

Step 1: Ask Claude Code to scaffold the script

In your terminal, start a Claude Code session and prompt it directly:

Write a Node.js script using Puppeteer that takes an HTML file path as input and outputs a PDF to ./output.pdf

Claude Code will write the script, detect that Puppeteer is not installed, and offer to run npm install puppeteer for you.

Step 2: Confirm dependency installation

Claude Code respects your project environment. It will ask before modifying package.json or running installs. You can approve once and it handles the rest. According to Anthropic's Claude Code documentation, the agent is designed to take real actions in your environment with user confirmation for side-effects like file writes and installs.

Step 3: Run and iterate

Once the script runs, you can iterate conversationally:

  • "Add A4 page format and 1cm margins"
  • "Use print media CSS only"
  • "Loop over all files in /reports and export each as a PDF"

Claude Code executes each revision in place, so you stay in flow without switching contexts.

How to generate a PDF with pdfkit (Node.js) in Claude Code

For programmatic layouts where you control every element (invoices, certificates, data tables), pdfkit is a better fit than Puppeteer. It has no browser dependency and runs faster.

Prompt Claude Code with something like:

Use pdfkit to create a PDF invoice from this JSON object: { customer: "Acme Corp", items: [...], total: 420.00 }. Output to ./invoice.pdf

Claude Code will write a script that maps your data structure to pdfkit's drawing API, add a header, line items, and totals, then run it. If the layout looks off, describe the fix in plain English and it adjusts.

When to use pdfkit vs Puppeteer

Use caseRecommended libraryWhy
HTML template to PDFPuppeteerPreserves CSS, fonts, and layout exactly
Programmatic invoice/reportpdfkitLightweight, no browser overhead
Markdown to PDFPuppeteer + markedConvert Markdown to HTML first, then render
Large batch exportpdfkit or html-pdf-nodeLower memory footprint at scale

How to convert Markdown to PDF in Claude Code

A common developer workflow is converting README files, documentation, or changelogs to PDF. Claude Code handles this in two steps: parse Markdown to HTML (using a library like marked), then render with Puppeteer.

Prompt:

Convert README.md to a styled PDF. Use marked for parsing and Puppeteer for rendering. Apply GitHub-style CSS.

Claude Code will fetch a CDN-hosted GitHub Markdown CSS, embed it in the HTML template, and produce a clean PDF that looks like a rendered GitHub README. This is useful for sending documentation to non-technical stakeholders. Learn more about README generation on Claude Code for related workflows.

How to use Claude Code slash commands during PDF workflows

Claude Code's slash commands help you navigate long sessions efficiently:

  • /clear: Reset context when switching between different PDF tasks
  • /usage: Check how many tokens you have remaining before starting a large batch job
  • /compact: Summarize conversation history to free up context for long iterative sessions

For a full reference, see the Claude Code slash commands list.

Why PDF generation jobs can burn through Claude Code usage fast

Generating a PDF script is not token-heavy. But iterating on layout, debugging Puppeteer issues, or asking Claude Code to process dozens of input files in a loop adds up. If you hit your usage limit mid-task, you face a multi-hour lockout and lose your session flow.

Claude Code's /usage command gives a snapshot, but it requires you to switch mental context to check it. Usagebar sits in your macOS menu bar and shows live usage with alerts at 50%, 75%, and 90% of your limit so you always know where you stand without leaving the terminal. It also shows exactly when your usage window resets, which matters when you're deciding whether to start a longer PDF batch job.

You can also check your usage at any time on claude.ai/settings/usage, though that requires opening a browser tab. See also: how to check Claude Code usage limits and when Claude Code usage resets.

Get Usagebar: instant download, pay what you want (free for students) at usagebar.com.

Key takeaways

  1. Use Puppeteer for HTML-to-PDF with full CSS fidelity; use pdfkit for programmatic data-driven documents
  2. Prompt Claude Code in plain English: it scaffolds the script, installs dependencies, and runs it in one session
  3. For Markdown-to-PDF, combine marked with Puppeteer and a CSS stylesheet
  4. Use /compact to preserve context in long iterative sessions, and /usage to check remaining quota before batch jobs
  5. Monitor live usage with Usagebar to avoid mid-task lockouts during multi-file PDF exports

Sources

Track Your Claude Code Usage

Never hit your usage limits unexpectedly. Usagebar lives in your menu bar and shows your 5-hour and weekly limits at a glance.

Get Usagebar