
Pdf To Markdown
- 899 installs
- 7 repo stars
- Updated June 18, 2026
- duc01226/easyplatform
pdf-to-markdown is an agent skill that converts native-text and scanned PDF files into clean Markdown for developers who need documentation, specs, or RAG-ready text extraction.
About
pdf-to-markdown is a document-processing agent skill in the duc01226/easyplatform repository that converts PDF files into clean Markdown for documentation, specifications, and agent ingestion pipelines. The skill handles both native-text PDFs and scanned documents that require OCR, and ships with disable-model-invocation set so agents invoke the conversion workflow directly instead of improvising extraction steps. Developers reach for pdf-to-markdown when PDFs block Markdown-first docs sites, spec reviews, or knowledge-base indexing because the source material is locked in binary page layout. The workflow auto-detects whether a file has selectable text or needs OCR, runs the bundled conversion script, and returns structured output with page counts and conversion mode metadata. Cross-platform Node.js execution keeps the pipeline portable across Windows, macOS, and Linux without external PDF desktop tools.
- Converts both native text PDFs and scanned documents using OCR
- Preserves structure, headings, lists and tables in Markdown output
- Designed for document processing workflows in AI coding agents
- Strict execution contract with step-by-step reporting
- Supports subagent authorization when OCR or complex extraction is required
Pdf To Markdown by the numbers
- 899 all-time installs (skills.sh)
- +15 installs in the week ending Aug 5, 2026 (Skillselion tracking)
- Ranked #290 of 1,879 Documentation skills by installs in the Skillselion catalog
- Security screen: CRITICAL risk (skills.sh audit)
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/duc01226/easyplatform --skill pdf-to-markdownAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 899 |
|---|---|
| repo stars | ★ 7 |
| Security audit | 2 / 3 scanners passed |
| Last updated | June 18, 2026 |
| Repository | duc01226/easyplatform ↗ |
How do you convert PDF files to Markdown?
Reliably convert native-text and scanned PDFs into clean Markdown for documentation, specs, or agent ingestion.
Who is it for?
Developers ingesting PDF specs, manuals, or scanned documents into Markdown docs sites, wikis, or agent knowledge bases.
Skip if: Developers who need pixel-perfect layout preservation, complex multi-column table fidelity, or interactive PDF form handling.
When should I use this skill?
A developer uploads or references a PDF that must become Markdown for documentation, specs, or agent context ingestion.
What you get
Clean Markdown files, JSON conversion metadata with page count and mode, and agent-ready text for docs or RAG pipelines.
- Markdown file
- JSON conversion metadata
By the numbers
- Handles native-text PDFs and scanned OCR documents in one skill workflow
- Sets disable-model-invocation so agents run the conversion script directly
Files
Codex compatibility note:
>
- Invoke repository skills with$skill-namein Codex; this mirrored copy rewrites legacy Claude/skill-namereferences.
- Task tracker mandate: BEFORE executing any workflow or skill step, create/update task tracking for all steps and keep it synchronized as progress changes.
- User-question prompts mean to ask the user directly in Codex.
- Ignore Claude-specific mode-switch instructions when they appear.
- Strict execution contract: when a user explicitly invokes a skill, execute that skill protocol as written.
- Subagent authorization: when a skill is user-invoked or AI-detected and its protocol requires subagents, that skill activation authorizes use of the required spawn_agent subagent(s) for that task.- Do not skip, reorder, or merge protocol steps unless the user explicitly approves the deviation first.
- For workflow skills, execute each listed child-skill step explicitly and report step-by-step evidence.
- If a required step/tool cannot run in this environment, stop and ask the user before adapting.
<!-- CODEX:PROJECT-REFERENCE-LOADING:START -->
Codex Project-Reference Loading (No Hooks)
Codex uses static project-reference loading instead of runtime-injected project docs. When coding, planning, debugging, testing, or reviewing, open project docs explicitly using this routing.
Always read:
docs/project-config.json(project-specific paths, commands, modules, and workflow/test settings)docs/project-reference/docs-index-reference.md(routes to the fulldocs/project-reference/*catalog)docs/project-reference/lessons.md(always-on guardrails and anti-patterns)
Missing/stale context route: If docs/project-config.json, the docs index, lessons.md, CLAUDE.md, AGENTS.md, or any task-required reference doc is missing or stale, auto-run $project-init or the narrow setup route ($project-config, $docs-init, $scan-all, $scan --target=<key>, $claude-md-init) before ordinary project-specific work. If Codex mirrors or AGENTS.md are missing/stale, ask the user to run $sync-codex; do not auto-run it.
Situation-based docs:
- Backend/CQRS/API/domain/entity changes:
backend-patterns-reference.md,domain-entities-reference.md,project-structure-reference.md - Frontend/UI/styling/design-system:
frontend-patterns-reference.md,scss-styling-guide.md,design-system/README.md - Spec authoring,
docs/specs/pathing, or TC format:feature-spec-reference.md,spec-system-reference.md,spec-principles.md - Behavior/public-contract changes or spec-test-code sync:
workflow-spec-test-code-cycle-reference.mdplus the spec docs above - Derived spec indexes/ERDs/reimplementation guides:
spec-system-reference.mdand source Feature Specs underdocs/specs/ - Integration test implementation/review:
integration-test-reference.md - E2E test implementation/review:
e2e-test-reference.md - Code review/audit work:
code-review-rules.mdplus domain docs above based on changed files
Do not read all docs blindly. Start from docs-index-reference.md, then open only relevant files for the task.
<!-- CODEX:PROJECT-REFERENCE-LOADING:END -->
Quick Summary
Goal: Convert PDF files to well-formatted Markdown with auto-detection of native text vs scanned documents.
Workflow:
1. Auto-Detect — Determine if PDF has native text or needs OCR 2. Convert — Run scripts/convert.cjs with input path and optional mode/output flags 3. Output — Returns JSON with success status, page count, and output path
Key Rules:
- Use
--mode auto(default) to let the tool decide native vs OCR - OCR for scanned PDFs requires additional
tesseract.jssetup - Complex multi-column layouts may not preserve structure perfectly
Be skeptical. Apply critical thinking, sequential thinking. Every claim needs traced proof, confidence percentages (Idea should be more than 80%).
pdf-to-markdown
Convert PDF files to Markdown format with automatic detection of native text vs scanned documents.
Installation Required
This skill requires npm dependencies. Run one of the following:
# Option 1: Install via ClaudeKit CLI (recommended)
ck init # Runs install.sh which handles all skills
# Option 2: Manual installation
cd .claude/skills/pdf-to-markdown
npm installDependencies: @opendocsg/pdf2md (native PDFs), pdfjs-dist (PDF parsing)
Note: OCR for scanned PDFs requires additional setup (see OCR section).
Quick Start
# Basic conversion (auto-detect native vs scanned)
node .claude/skills/pdf-to-markdown/scripts/convert.cjs --input ./document.pdf
# Specify output path
node .claude/skills/pdf-to-markdown/scripts/convert.cjs -i ./doc.pdf -o ./output.md
# Force native mode (skip OCR detection)
node .claude/skills/pdf-to-markdown/scripts/convert.cjs -i ./doc.pdf --mode nativeCLI Options
| Option | Short | Description | Default |
|---|---|---|---|
--input | -i | Input PDF file path | (required) |
--output | -o | Output markdown file path | {input}.md |
--mode | -m | Conversion mode: auto, native, ocr | auto |
--help | -h | Show help message |
Features
- Auto-Detection: Automatically determines if PDF has native text or requires OCR
- Native PDFs: Fast extraction using @opendocsg/pdf2md
- Tables: Basic table structure preservation
- Cross-OS: Works on Windows, macOS, Linux
- No System Dependencies: Pure JavaScript implementation
Conversion Modes
Auto (Default)
Checks if PDF has extractable text on first page. Uses native extraction if text found, otherwise falls back to OCR warning.
Native
Fast direct text extraction. Best for PDFs with selectable text (not scanned images).
OCR (Scanned PDFs) - Coming Soon
For scanned documents. Currently not implemented - the skill will notify you if a PDF appears to be scanned.
Output
Returns JSON on success:
{
"success": true,
"input": "/path/to/input.pdf",
"output": "/path/to/output.md",
"stats": {
"pages": 5,
"mode": "native"
}
}Limitations
- Complex multi-column layouts may not preserve structure
- Scanned PDF OCR accuracy depends on image quality
- Mathematical formulas may not convert perfectly
- First-run OCR downloads language data (~15MB)
OCR Setup (Optional)
For scanned PDF support, install additional dependencies:
npm install tesseract.js pdfjs-dist canvasNote: The canvas package may require build tools on some systems.
---
[IMPORTANT] Use task tracking to break ALL work into small tasks BEFORE starting — including tasks for each file read. This prevents context loss from long files. For simple tasks, AI MUST ATTENTION ask user whether to skip.
<!-- SYNC:ai-mistake-prevention -->
AI Mistake Prevention — Failure modes to avoid on every task:
>
Re-read files after context changes. Context compaction, resume, or long-running work can make memory stale; verify current files before acting.
Verify generated content against source evidence. AI hallucinates APIs, names, claims, and document facts. Check the relevant source before documenting or referencing.
Check downstream references before deleting or renaming. Removing an artifact can stale docs, generated mirrors, configs, and callers; map references first.
Trace the full impact chain after edits. Changing a definition can miss derived outputs and consumers. Follow the affected chain before declaring done.
Verify ALL affected outputs, not just the first. One green check is not all green checks; validate every output surface the change can affect.
Assume existing values are intentional — ask WHY before changing. Before changing a constant, limit, flag, wording, or pattern, read nearby context and history.
Surface ambiguity before acting — don't pick silently. Multiple valid interpretations require an explicit question or stated assumption with risk.
Keep shared guidance role-relevant. Universal guidance must help every receiving skill or agent; code-specific obligations belong only in code-specific protocols.
<!-- /SYNC:ai-mistake-prevention -->
<!-- SYNC:critical-thinking-mindset -->
Critical Thinking Mindset — Apply critical thinking, sequential thinking. Every claim needs traced proof, confidence >80% to act.
Anti-hallucination: Never present guess as fact — cite sources for every claim, admit uncertainty freely, self-check output for errors, cross-reference independently, stay skeptical of own confidence — certainty without evidence root of all hallucination.
<!-- /SYNC:critical-thinking-mindset -->
<!-- SYNC:critical-thinking-mindset:reminder -->
MUST ATTENTION apply critical + sequential thinking — every claim needs appropriate traced evidence (file:line for repo/code claims; source URL or artifact section for research, product, content, and docs claims); confidence >80% to act, <60% DO NOT recommend. Anti-hallucination: never present guess as fact, admit uncertainty freely, cross-reference independently, stay skeptical of own confidence.
<!-- /SYNC:critical-thinking-mindset:reminder -->
<!-- SYNC:ai-mistake-prevention:reminder -->
MUST ATTENTION apply AI mistake prevention — verify generated content against evidence, trace downstream references before deleting or renaming, verify all affected outputs, re-read files after context loss, and surface ambiguity before acting.
<!-- /SYNC:ai-mistake-prevention:reminder -->
Closing Reminders
IMPORTANT MUST ATTENTION Goal: Convert PDF files to well-formatted Markdown with auto-detection of native text vs scanned documents.
Protocols in force (concise digest of the SYNC/shared blocks this skill carries):
- AI Mistake Prevention: verify generated content against evidence, trace downstream references, verify all affected outputs, re-read after context loss, surface ambiguity.
- Critical Thinking: Sequential thinking, traced
file:lineproof, confidence >80% to act.
IMPORTANT MUST ATTENTION break work into small todo tasks using task tracking BEFORE starting IMPORTANT MUST ATTENTION search codebase for 3+ similar patterns before creating new code IMPORTANT MUST ATTENTION cite file:line evidence for every claim (confidence >80% to act) IMPORTANT MUST ATTENTION add a final review todo task to verify work quality
[TASK-PLANNING] Before acting, analyze task scope and systematically break it into small todo tasks and sub-tasks using task tracking.
<!-- CODEX:SYNC-PROMPT-PROTOCOLS:START -->
Hookless Prompt Protocol Mirror (Auto-Synced)
Source: .claude/.ck.json + .claude/skills/shared/sync-inline-versions.md (:full blocks) + .claude/scripts/lib/hookless-prompt-protocol.cjs
[WORKFLOW-EXECUTION-PROTOCOL] [BLOCKING] Workflow Execution Protocol — MANDATORY IMPORTANT MUST CRITICAL. Do not skip for any reason.
Generic portability boundary: Reusable skills and protocol text stay project-neutral; project-specific conventions are discovered from docs/project-config.json and docs/project-reference/. Apply shared AI-SDD from shared/sdd-artifact-contract.md. Read docs/project-config.json and docs/project-reference/docs-index-reference.md, then open the project reference docs named there. For spec, test-case, behavior-change, public-contract, or docs/specs/ work, route through the local spec docs named by the docs index: feature-spec-reference.md, spec-system-reference.md, spec-principles.md, and workflow-spec-test-code-cycle-reference.md when specs/tests/code must stay synchronized. If either file or a required reference doc is missing or stale, auto-run $project-init (or the narrow lower-level route such as $project-config, $docs-init, $scan-all, or $scan --target=<key>) before ordinary project-specific work. Any supported AI tool may execute when this shared context and local docs are available.
1. DETECT: If the prompt starts with an explicit slash skill/workflow command, execute it directly. Otherwise match the prompt against the workflow catalog and skill list. 2. ANALYZE: Choose the best option: execute directly, invoke a skill, activate a standard workflow, or compose a custom step combination. 3. AUTO-SELECT: Pick the best option yourself. Do not ask the user to choose between direct execution, skill, standard workflow, or custom workflow. 4. ACTIVATE: For a selected workflow, call $start-workflow <workflowId>; for a selected skill, invoke that skill; for a custom workflow, sequence custom steps directly; for direct execution, proceed with the task. 5. CREATE TASKS: task tracking for ALL workflow/skill/custom steps before execution when the selected path has multiple steps. 6. EXECUTE: Advance per the Workflow Step Advancement & Parallel Phases rule in your context instructions — model-driven; a sub-agent completion advances a step identically to an inline call; a parallel-phase group is an all-return barrier (advance only after ALL members return, never serialize it)
Shared AI-SDD Protocol Markers
Source: .claude/skills/shared/sync-inline-versions.md
SYNC:ai-sdd-artifact-contract
AI-SDD Artifact Contract — Shared spec-driven development rules stay portable and source-owned.
>
1. Keep reusable AI-SDD principles in .claude; put repository-specific paths, commands, owners, products, and formats in project config/reference docs.2. Preserve cycle: spec -> plan -> tasks -> implement -> verify -> update spec/docs.3. Trace every requirement or invariant through decision, task, TC/test, source evidence, and docs/spec update.
4. Treat code-to-spec extraction as reference-only until accepted by the canonical spec owner.
5. Any supported AI tool may plan, implement, review, or verify with synced context; using multiple tools is optional.
6. Update.claudesource first, then sync generated mirrors; do not manually edit.agents,.codex, orAGENTS.md. — why: mirrors are generated artifacts; hand-edits are overwritten on the next sync
7. Ifdocs/project-config.json, root instruction files, or a required project-reference doc is missing or stale, auto-run$project-initor the narrow lower-level route before ordinary project-specific work.
>
Active reference: shared/sdd-artifact-contract.md in the active skills root.---
SYNC:ai-sdd-artifact-contract:reminder
- MANDATORY Apply
shared/sdd-artifact-contract.md; keep reusable AI-SDD in.claudeand local rules in project docs. - MANDATORY Code-to-spec extraction is reference-only until canonical acceptance; any supported AI tool may execute with synced context.
- MANDATORY Update
.claudesource before syncing generated mirrors; do not manually edit.agents,.codex, orAGENTS.md. - MANDATORY Missing or stale project config, root instruction files, or required reference docs route project-specific work through
$project-initor the narrow setup route automatically.
[TASK-PLANNING] [MANDATORY] BEFORE executing any workflow or skill step, create/update task tracking for all planned steps, then keep it synchronized as each step starts/completes.
[LESSON-LEARNED-REMINDER] [BLOCKING] Task Planning & Continuous Improvement — MANDATORY. Do not skip.
Break work into small tasks (task tracking) before starting. Add final task: "Analyze AI mistakes & lessons learned".
Extract lessons — ROOT CAUSE ONLY, not symptom fixes:
1. Name the FAILURE MODE (reasoning/assumption failure), not symptom — "assumed API existed without reading source" not "used wrong enum value". 2. Generality test: does this failure mode apply to ≥3 contexts/codebases? If not, abstract one level up. 3. Write as a universal rule — strip project-specific names/paths/classes. Useful on any codebase. 4. Consolidate: multiple mistakes sharing one failure mode → ONE lesson. 5. Recurrence gate: "Would this recur in future session WITHOUT this reminder?" — No → skip $learn. 6. Auto-fix gate: "Could $code-review/$code-simplifier/$security-review/$lint catch this?" — Yes → improve review skill instead. 7. BOTH gates pass → ask user to run $learn. [CRITICAL-THINKING-MINDSET] Apply critical thinking, sequential thinking. Every claim needs traced proof, confidence >80% to act. Anti-hallucination principle: Never present guess as fact — cite sources for every claim, admit uncertainty freely, self-check output for errors, cross-reference independently, stay skeptical of own confidence — certainty without evidence root of all hallucination. AI Attention principle (Primacy-Recency): Put the 3 most critical rules at both top and bottom of long prompts/protocols so instruction adherence survives long context windows. Goal-driven execution: Define success criteria first, loop until verified, and stop only when observable checks pass. Tests verify intent: Tests must protect business rules/invariants and fail when the protected intent breaks, not only mirror current behavior.
Common AI Mistake Prevention (System Lessons)
- Re-read files after context compaction. Edit requires prior Read in same context; compaction wipes read state. Re-read before editing.
- Grep for old terms after bulk replacements. AI over-trusts find/replace completeness. Grep full repo after bulk edits for missed refs in docs/configs/catalogs.
- Check downstream references before deleting. Deletions cascade doc/code staleness. Map referencing files before removal.
- After memory loss, check existing state before creating new. Compaction wipes prior-work memory. Query current state to resume — never blindly duplicate.
- Verify AI-generated content against actual code. AI hallucinates APIs, class names, method signatures. Grep to confirm existence before documenting/referencing.
- Trace full dependency chain after edits. Changing a definition misses downstream consumers. Trace the full chain.
- When renaming, grep ALL consumer file types. Some file types silently ignore missing refs (no compile error). Search code, templates, configs, generated files.
- Trace ALL code paths when verifying correctness. Code existing ≠ code executing. Trace early exits, error branches, conditional skips — not just happy path.
- Update docs that embed canonical data when source changes. Docs inlining derived data (workflows, schemas, configs) go stale silently. Update all embedding docs alongside source.
- Verify sub-agent results after context recovery. Background agents may finish while parent compacted — grep-verify output, don't trust assumed completion.
- Cross-check full target list against sub-agent assignments. Parallel sub-agents by category miss boundary items. Reconcile union of assignments against target list before proceeding.
- Sub-agents inherit knowledge only from their agent .md definition — use custom agent types, not built-in Explore. Tool adoption = permission + knowledge + enforcement (numbered workflow step).
- Persist sub-agent findings incrementally, not as a final batch. Long sub-agents hit cutoffs before final write — findings lost. Instruct append-per-section to report file.
- When debugging, ask "whose responsibility?" before fixing. Trace caller (wrong data) vs callee (wrong handling). Fix at responsible layer — never patch symptom site.
- Grep ALL removed names after extraction/refactoring. Primary file "done" ≠ secondary files clean. Grep entire scope for every removed symbol before declaring complete.
- Assume existing values are intentional — ask WHY before changing. Pattern-matching as "wrong" skips context. Before changing any constant/limit/flag: read comments, git blame, surrounding code.
- Verify ALL affected outputs, not just the first. One build green ≠ all green. Multi-stack changes (backend/frontend/tests/docs) require verifying EVERY output.
- Evaluate fit before copying a nearby pattern. Closest example ≠ matching preconditions — verify the new context shares the same constraints, base classes, scope, lifetime.
- Holistic-first debugging — resist nearest-attention trap. Don't dive into first plausible cause. List EVERY precondition (config, env vars, paths, DB, endpoints, creds, versions, DI, data). Verify each against evidence (grep/query — not reasoning). Ask "what would falsify this?" — if nothing, it's not a hypothesis. Most expensive failure: going deeper in "obvious" layer while bug sits in layer never questioned.
- Surgical changes — apply the diff test (context-aware). Two modes: (1) Bug fix → every line traces to the bug; no restyling; orphan cleanup only for imports YOUR changes made unused. (2) Review/enhancement → implement improvements AND announce as "Enhancement beyond main request: [what]". Never silently scope-creep. Diff test: "Would this line exist if I wasn't asked to do X?" — if no, delete or announce.
- Surface ambiguity before coding — don't pick silently. Multiple valid interpretations → present each with effort: "[Request] could mean (1) [N h], (2) [N h]. Which matters?" List scope/format/volume/constraints assumptions first. If simpler path exists, say so. Never silently pick.
- [MANDATORY FIRST ACTION] ALWAYS activate a suitable skill or workflow BEFORE responding. Match task against workflow catalog + skill list; invoke via skill invocation or
$start-workflow <workflowId>. NEVER answer or write code before checking. Skip = protocol violation. - Why-Review adversarial mindset — apply when reviewing any plan, decision, or design. Default SKEPTIC not VALIDATOR: steel-man a rejected alternative, invert each stated reason ("what does it sacrifice?"), stress-test top 2-3 assumptions, run pre-mortem ("ships, fails in 3 months — what breaks?"), surface 1-2 alternatives author missed. Section presence ≠ quality; quality = causal reasoning + concrete mitigations + evidence, not "it's better" or "monitor closely".
- Front-load report-write in sub-agent prompts for large reviews. Many-file sub-agents hit budget before final write — findings lost. Design prompts so: (1) report-write is first explicit deliverable, (2) append per-file/section (not batched), (3) scope bounded so reads don't exhaust budget. Truncated mid-sentence with no report file → spawn narrower scope, don't retry same prompt.
- After context compaction, re-verify all prior phase outcomes before continuing. Summaries describe intent, not environment state (git index, filesystem, processes). On resume, FIRST audit: git status, re-read modified files, verify filesystem. Every "completed" claim is an untested hypothesis until evidence confirms.
- OOM/memory: check row count before row size. Triage: (1) Unbounded query — no DB filter for trigger? Push filter to DB; eliminates OOM. (2) Large rows? Projection reduces proportionally. Row reduction > projection in ROI.
- Keep domain concepts out of generic/shared/infrastructure layers. Reusable layer (shared library, framework, infra module) must reference NO consumer-specific domain concept — tenant/customer/product IDs, business entities, feature rules. Leak compiles + runs → passes review silently while coupling the "reusable" layer to one consumer. Keep shared type domain-free; push domain fields/logic down into the consumer via subclass/composition. — why: a layer coupled to one consumer's domain is no longer reusable.
<!-- CODEX:SYNC-PROMPT-PROTOCOLS:END -->
{
"name": "pdf-to-markdown",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "pdf-to-markdown",
"version": "1.0.0",
"dependencies": {
"@opendocsg/pdf2md": "^0.1.28"
},
"engines": {
"node": ">=18.0.0"
},
"optionalDependencies": {
"pdfjs-dist": "^4.10.38",
"tesseract.js": "^5.1.1"
}
},
"node_modules/@napi-rs/canvas": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas/-/canvas-0.1.88.tgz",
"integrity": "sha512-/p08f93LEbsL5mDZFQ3DBxcPv/I4QG9EDYRRq1WNlCOXVfAHBTHMSVMwxlqG/AtnSfUr9+vgfN7MKiyDo0+Weg==",
"license": "MIT",
"optional": true,
"workspaces": [
"e2e/*"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
},
"optionalDependencies": {
"@napi-rs/canvas-android-arm64": "0.1.88",
"@napi-rs/canvas-darwin-arm64": "0.1.88",
"@napi-rs/canvas-darwin-x64": "0.1.88",
"@napi-rs/canvas-linux-arm-gnueabihf": "0.1.88",
"@napi-rs/canvas-linux-arm64-gnu": "0.1.88",
"@napi-rs/canvas-linux-arm64-musl": "0.1.88",
"@napi-rs/canvas-linux-riscv64-gnu": "0.1.88",
"@napi-rs/canvas-linux-x64-gnu": "0.1.88",
"@napi-rs/canvas-linux-x64-musl": "0.1.88",
"@napi-rs/canvas-win32-arm64-msvc": "0.1.88",
"@napi-rs/canvas-win32-x64-msvc": "0.1.88"
}
},
"node_modules/@napi-rs/canvas-android-arm64": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-android-arm64/-/canvas-android-arm64-0.1.88.tgz",
"integrity": "sha512-KEaClPnZuVxJ8smUWjV1wWFkByBO/D+vy4lN+Dm5DFH514oqwukxKGeck9xcKJhaWJGjfruGmYGiwRe//+/zQQ==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"android"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-darwin-arm64": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-darwin-arm64/-/canvas-darwin-arm64-0.1.88.tgz",
"integrity": "sha512-Xgywz0dDxOKSgx3eZnK85WgGMmGrQEW7ZLA/E7raZdlEE+xXCozobgqz2ZvYigpB6DJFYkqnwHjqCOTSDGlFdg==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-darwin-x64": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-darwin-x64/-/canvas-darwin-x64-0.1.88.tgz",
"integrity": "sha512-Yz4wSCIQOUgNucgk+8NFtQxQxZV5NO8VKRl9ePKE6XoNyNVC8JDqtvhh3b3TPqKK8W5p2EQpAr1rjjm0mfBxdg==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-linux-arm-gnueabihf": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm-gnueabihf/-/canvas-linux-arm-gnueabihf-0.1.88.tgz",
"integrity": "sha512-9gQM2SlTo76hYhxHi2XxWTAqpTOb+JtxMPEIr+H5nAhHhyEtNmTSDRtz93SP7mGd2G3Ojf2oF5tP9OdgtgXyKg==",
"cpu": [
"arm"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-linux-arm64-gnu": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm64-gnu/-/canvas-linux-arm64-gnu-0.1.88.tgz",
"integrity": "sha512-7qgaOBMXuVRk9Fzztzr3BchQKXDxGbY+nwsovD3I/Sx81e+sX0ReEDYHTItNb0Je4NHbAl7D0MKyd4SvUc04sg==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-linux-arm64-musl": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-arm64-musl/-/canvas-linux-arm64-musl-0.1.88.tgz",
"integrity": "sha512-kYyNrUsHLkoGHBc77u4Unh067GrfiCUMbGHC2+OTxbeWfZkPt2o32UOQkhnSswKd9Fko/wSqqGkY956bIUzruA==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-linux-riscv64-gnu": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-riscv64-gnu/-/canvas-linux-riscv64-gnu-0.1.88.tgz",
"integrity": "sha512-HVuH7QgzB0yavYdNZDRyAsn/ejoXB0hn8twwFnOqUbCCdkV+REna7RXjSR7+PdfW0qMQ2YYWsLvVBT5iL/mGpw==",
"cpu": [
"riscv64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-linux-x64-gnu": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-x64-gnu/-/canvas-linux-x64-gnu-0.1.88.tgz",
"integrity": "sha512-hvcvKIcPEQrvvJtJnwD35B3qk6umFJ8dFIr8bSymfrSMem0EQsfn1ztys8ETIFndTwdNWJKWluvxztA41ivsEw==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-linux-x64-musl": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-linux-x64-musl/-/canvas-linux-x64-musl-0.1.88.tgz",
"integrity": "sha512-eSMpGYY2xnZSQ6UxYJ6plDboxq4KeJ4zT5HaVkUnbObNN6DlbJe0Mclh3wifAmquXfrlgTZt6zhHsUgz++AK6g==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-win32-arm64-msvc": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-win32-arm64-msvc/-/canvas-win32-arm64-msvc-0.1.88.tgz",
"integrity": "sha512-qcIFfEgHrchyYqRrxsCeTQgpJZ/GqHiqPcU/Fvw/ARVlQeDX1VyFH+X+0gCR2tca6UJrq96vnW+5o7buCq+erA==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@napi-rs/canvas-win32-x64-msvc": {
"version": "0.1.88",
"resolved": "https://registry.npmjs.org/@napi-rs/canvas-win32-x64-msvc/-/canvas-win32-x64-msvc-0.1.88.tgz",
"integrity": "sha512-ROVqbfS4QyZxYkqmaIBBpbz/BQvAR+05FXM5PAtTYVc0uyY8Y4BHJSMdGAaMf6TdIVRsQsiq+FG/dH9XhvWCFQ==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"engines": {
"node": ">= 10"
},
"funding": {
"type": "github",
"url": "https://github.com/sponsors/Brooooooklyn"
}
},
"node_modules/@opendocsg/pdf2md": {
"version": "0.1.32",
"resolved": "https://registry.npmjs.org/@opendocsg/pdf2md/-/pdf2md-0.1.32.tgz",
"integrity": "sha512-UK4qVuesmUcpPZXMeO8FwRqpCNwJRBTHcae4j+3Mr3bxrNqilZIIowdrzgcgn8fSQ2Dg/P4/0NoPkxAvf9D5rw==",
"license": "MIT",
"dependencies": {
"enumify": "^1.0.4",
"minimist": "^1.2.5",
"pdfjs-dist": "^4.2.67"
},
"bin": {
"pdf2md": "lib/pdf2md-cli.js"
}
},
"node_modules/bmp-js": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/bmp-js/-/bmp-js-0.1.0.tgz",
"integrity": "sha512-vHdS19CnY3hwiNdkaqk93DvjVLfbEcI8mys4UjuWrlX1haDmroo8o4xCzh4wD6DGV6HxRCyauwhHRqMTfERtjw==",
"license": "MIT",
"optional": true
},
"node_modules/enumify": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/enumify/-/enumify-1.0.4.tgz",
"integrity": "sha512-5mwWXaVzJaqyUdOW/PDH5QySRgmQ8VvujmxmvXoXj9w0n+6omhVuyD56eI37FMqy/LxueJzsQ4DrHVQzuT/TXg==",
"license": "MIT"
},
"node_modules/idb-keyval": {
"version": "6.2.2",
"resolved": "https://registry.npmjs.org/idb-keyval/-/idb-keyval-6.2.2.tgz",
"integrity": "sha512-yjD9nARJ/jb1g+CvD0tlhUHOrJ9Sy0P8T9MF3YaLlHnSRpwPfpTX0XIvpmw3gAJUmEu3FiICLBDPXVwyEvrleg==",
"license": "Apache-2.0",
"optional": true
},
"node_modules/is-electron": {
"version": "2.2.2",
"resolved": "https://registry.npmjs.org/is-electron/-/is-electron-2.2.2.tgz",
"integrity": "sha512-FO/Rhvz5tuw4MCWkpMzHFKWD2LsfHzIb7i6MdPYZ/KW7AlxawyLkqdy+jPZP1WubqEADE3O4FUENlJHDfQASRg==",
"license": "MIT",
"optional": true
},
"node_modules/is-url": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/is-url/-/is-url-1.2.4.tgz",
"integrity": "sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==",
"license": "MIT",
"optional": true
},
"node_modules/minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/node-fetch": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"license": "MIT",
"optional": true,
"dependencies": {
"whatwg-url": "^5.0.0"
},
"engines": {
"node": "4.x || >=6.0.0"
},
"peerDependencies": {
"encoding": "^0.1.0"
},
"peerDependenciesMeta": {
"encoding": {
"optional": true
}
}
},
"node_modules/opencollective-postinstall": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.3.tgz",
"integrity": "sha512-8AV/sCtuzUeTo8gQK5qDZzARrulB3egtLzFgteqB2tcT4Mw7B8Kt7JcDHmltjz6FOAHsvTevk70gZEbhM4ZS9Q==",
"license": "MIT",
"optional": true,
"bin": {
"opencollective-postinstall": "index.js"
}
},
"node_modules/pdfjs-dist": {
"version": "4.10.38",
"resolved": "https://registry.npmjs.org/pdfjs-dist/-/pdfjs-dist-4.10.38.tgz",
"integrity": "sha512-/Y3fcFrXEAsMjJXeL9J8+ZG9U01LbuWaYypvDW2ycW1jL269L3js3DVBjDJ0Up9Np1uqDXsDrRihHANhZOlwdQ==",
"license": "Apache-2.0",
"engines": {
"node": ">=20"
},
"optionalDependencies": {
"@napi-rs/canvas": "^0.1.65"
}
},
"node_modules/regenerator-runtime": {
"version": "0.13.11",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz",
"integrity": "sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==",
"license": "MIT",
"optional": true
},
"node_modules/tesseract.js": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/tesseract.js/-/tesseract.js-5.1.1.tgz",
"integrity": "sha512-lzVl/Ar3P3zhpUT31NjqeCo1f+D5+YfpZ5J62eo2S14QNVOmHBTtbchHm/YAbOOOzCegFnKf4B3Qih9LuldcYQ==",
"hasInstallScript": true,
"license": "Apache-2.0",
"optional": true,
"dependencies": {
"bmp-js": "^0.1.0",
"idb-keyval": "^6.2.0",
"is-electron": "^2.2.2",
"is-url": "^1.2.4",
"node-fetch": "^2.6.9",
"opencollective-postinstall": "^2.0.3",
"regenerator-runtime": "^0.13.3",
"tesseract.js-core": "^5.1.1",
"wasm-feature-detect": "^1.2.11",
"zlibjs": "^0.3.1"
}
},
"node_modules/tesseract.js-core": {
"version": "5.1.1",
"resolved": "https://registry.npmjs.org/tesseract.js-core/-/tesseract.js-core-5.1.1.tgz",
"integrity": "sha512-KX3bYSU5iGcO1XJa+QGPbi+Zjo2qq6eBhNjSGR5E5q0JtzkoipJKOUQD7ph8kFyteCEfEQ0maWLu8MCXtvX5uQ==",
"license": "Apache-2.0",
"optional": true
},
"node_modules/tr46": {
"version": "0.0.3",
"resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz",
"integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==",
"license": "MIT",
"optional": true
},
"node_modules/wasm-feature-detect": {
"version": "1.8.0",
"resolved": "https://registry.npmjs.org/wasm-feature-detect/-/wasm-feature-detect-1.8.0.tgz",
"integrity": "sha512-zksaLKM2fVlnB5jQQDqKXXwYHLQUVH9es+5TOOHwGOVJOCeRBCiPjwSg+3tN2AdTCzjgli4jijCH290kXb/zWQ==",
"license": "Apache-2.0",
"optional": true
},
"node_modules/webidl-conversions": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz",
"integrity": "sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==",
"license": "BSD-2-Clause",
"optional": true
},
"node_modules/whatwg-url": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz",
"integrity": "sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==",
"license": "MIT",
"optional": true,
"dependencies": {
"tr46": "~0.0.3",
"webidl-conversions": "^3.0.0"
}
},
"node_modules/zlibjs": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/zlibjs/-/zlibjs-0.3.1.tgz",
"integrity": "sha512-+J9RrgTKOmlxFSDHo0pI1xM6BLVUv+o0ZT9ANtCxGkjIVCCUdx9alUF8Gm+dGLKbkkkidWIHFDZHDMpfITt4+w==",
"license": "MIT",
"optional": true,
"engines": {
"node": "*"
}
}
}
}
{
"name": "pdf-to-markdown",
"version": "1.0.0",
"description": "Convert PDF files to Markdown with native text and OCR support",
"main": "scripts/convert.cjs",
"scripts": {
"start": "node scripts/convert.cjs",
"test": "node tests/run-tests.cjs"
},
"dependencies": {
"@opendocsg/pdf2md": "^0.1.28"
},
"optionalDependencies": {
"pdfjs-dist": "^4.10.38",
"tesseract.js": "^5.1.1"
},
"engines": {
"node": ">=18.0.0"
},
"private": true
}
#!/usr/bin/env node
/**
* PDF to Markdown Converter CLI
*
* Usage:
* node convert.cjs --input ./doc.pdf [--output ./out.md] [--mode auto|native|ocr]
*
* Options:
* --input, -i Input PDF file (required)
* --output, -o Output markdown path (default: input.md)
* --mode, -m Conversion mode: auto, native, ocr (default: auto)
* --help, -h Show help message
*/
const path = require('path');
/**
* Parse command line arguments
* @param {string[]} argv
* @returns {object}
*/
function parseArgs(argv) {
const args = {
input: null,
output: null,
mode: 'auto',
help: false
};
for (let i = 2; i < argv.length; i++) {
const arg = argv[i];
const nextArg = argv[i + 1];
switch (arg) {
case '--input':
case '-i':
args.input = nextArg;
i++;
break;
case '--output':
case '-o':
args.output = nextArg;
i++;
break;
case '--mode':
case '-m':
args.mode = nextArg;
i++;
break;
case '--help':
case '-h':
args.help = true;
break;
default:
// Positional argument = input file
if (!arg.startsWith('-') && !args.input) {
args.input = arg;
}
}
}
return args;
}
/**
* Print help message
*/
function printHelp() {
console.log(`
pdf-to-markdown - Convert PDF files to Markdown
USAGE:
node convert.cjs --input <file.pdf> [options]
node convert.cjs <file.pdf> [options]
OPTIONS:
--input, -i <path> Input PDF file (required)
--output, -o <path> Output markdown path (default: same as input with .md)
--mode, -m <mode> Conversion mode: auto, native, ocr (default: auto)
--help, -h Show this help message
MODES:
auto Auto-detect if PDF has native text or needs OCR (default)
native Fast extraction for PDFs with selectable text
ocr Use OCR for scanned documents (requires tesseract.js)
EXAMPLES:
# Basic conversion (auto-detect mode)
node convert.cjs --input ./document.pdf
# With custom output
node convert.cjs -i ./report.pdf -o ./output/report.md
# Force native mode (skip detection)
node convert.cjs -i ./doc.pdf --mode native
OUTPUT:
Returns JSON on success:
{
"success": true,
"input": "/path/to/input.pdf",
"output": "/path/to/output.md",
"stats": { "pages": 5, "mode": "native" }
}
EXIT CODES:
0 Success
1 Error
`);
}
/**
* Print result as JSON
* @param {object} result
*/
function printResult(result) {
console.log(JSON.stringify(result, null, 2));
}
/**
* Validate arguments
* @param {object} args
* @returns {{valid: boolean, error?: string}}
*/
function validateArgs(args) {
if (!args.input) {
return {
valid: false,
error: 'Input file required. Use --input <path> or provide positional argument.'
};
}
const validModes = ['auto', 'native', 'ocr'];
if (!validModes.includes(args.mode)) {
return {
valid: false,
error: `Invalid mode: ${args.mode}. Must be one of: ${validModes.join(', ')}`
};
}
return { valid: true };
}
/**
* Check if dependencies are installed
* @returns {{available: boolean, missing: string[]}}
*/
function checkDependencies() {
const missing = [];
try {
require.resolve('@opendocsg/pdf2md');
} catch {
missing.push('@opendocsg/pdf2md');
}
return {
available: missing.length === 0,
missing
};
}
/**
* Main entry point
*/
async function main() {
const args = parseArgs(process.argv);
if (args.help) {
printHelp();
process.exit(0);
}
const validation = validateArgs(args);
if (!validation.valid) {
printResult({ success: false, error: validation.error });
process.exit(1);
}
const deps = checkDependencies();
if (!deps.available) {
printResult({
success: false,
error: `Missing dependencies: ${deps.missing.join(', ')}. Run 'npm install' in skill directory.`,
hint: `cd ${path.dirname(__dirname)} && npm install`
});
process.exit(1);
}
// Import converter (lazy load after dependency check)
const { convert } = require('./lib/converter.cjs');
try {
const result = await convert({
input: args.input,
output: args.output,
mode: args.mode
});
printResult(result);
process.exit(result.success ? 0 : 1);
} catch (error) {
printResult({
success: false,
error: error.message || 'Unexpected error during conversion'
});
process.exit(1);
}
}
// Run main
main().catch(error => {
console.error(JSON.stringify({
success: false,
error: `Unhandled error: ${error.message}`
}));
process.exit(1);
});
/**
* Core PDF to Markdown converter
* Supports native text PDFs and scanned documents (OCR)
*/
const fs = require('fs');
const path = require('path');
const { resolveOutputPath } = require('./output-handler.cjs');
const { detectPdfType, resolveMode } = require('./pdf-detector.cjs');
/**
* Convert PDF file to Markdown
*
* @param {object} options
* @param {string} options.input - Input PDF file path
* @param {string|null} options.output - Output markdown path (optional)
* @param {string} options.mode - Conversion mode: 'auto', 'native', 'ocr'
* @returns {Promise<{success: boolean, input: string, output: string, stats?: object, error?: string}>}
*/
async function convert(options) {
const { input, output, mode = 'auto' } = options;
// Validate input
if (!input) {
return { success: false, error: 'Input file path is required' };
}
const absoluteInput = path.resolve(input);
if (!fs.existsSync(absoluteInput)) {
return { success: false, error: `Input file not found: ${absoluteInput}` };
}
// Check file extension
const ext = path.extname(absoluteInput).toLowerCase();
if (ext !== '.pdf') {
return { success: false, error: `Invalid file type: ${ext}. Expected .pdf` };
}
try {
// Detect PDF type
const detection = await detectPdfType(absoluteInput);
const effectiveMode = resolveMode(mode, detection);
// Track statistics
const stats = { pages: 0, mode: effectiveMode };
// Resolve output path
const outputPath = resolveOutputPath(absoluteInput, output, '.md');
let markdown;
if (effectiveMode === 'native') {
markdown = await convertNative(absoluteInput, stats);
} else {
// OCR mode
const ocrResult = await convertOcr(absoluteInput, stats);
if (!ocrResult.success) {
return ocrResult;
}
markdown = ocrResult.markdown;
}
// Write output
fs.writeFileSync(outputPath, markdown, 'utf8');
// Verify output
if (!fs.existsSync(outputPath)) {
return { success: false, error: 'Markdown generation failed - no output file created' };
}
return {
success: true,
input: absoluteInput,
output: outputPath,
stats
};
} catch (error) {
return {
success: false,
error: error.message || 'Unknown conversion error',
stack: process.env.DEBUG ? error.stack : undefined
};
}
}
/**
* Convert native text PDF to Markdown
* @param {string} pdfPath - Path to PDF file
* @param {object} stats - Statistics object to update
* @returns {Promise<string>} Markdown content
*/
async function convertNative(pdfPath, stats) {
let pdf2md;
try {
pdf2md = require('@opendocsg/pdf2md');
} catch (err) {
throw new Error('@opendocsg/pdf2md not installed. Run `npm install` in the skill directory.');
}
const pdfBuffer = fs.readFileSync(pdfPath);
const result = await pdf2md(pdfBuffer);
// Extract page count from result if available
if (result && typeof result === 'string') {
// Estimate pages from content length (rough heuristic)
stats.pages = Math.max(1, Math.ceil(result.length / 3000));
return result;
}
throw new Error('Failed to extract text from PDF');
}
/**
* Convert scanned PDF using OCR
* @param {string} pdfPath - Path to PDF file
* @param {object} stats - Statistics object to update
* @returns {Promise<{success: boolean, markdown?: string, error?: string}>}
*/
async function convertOcr(pdfPath, stats) {
// Check if OCR dependencies are available
let tesseract;
try {
tesseract = require('tesseract.js');
} catch (err) {
return {
success: false,
error: 'OCR mode requires tesseract.js. Install with: npm install tesseract.js pdfjs-dist canvas',
hint: 'For native text PDFs, use --mode native'
};
}
// OCR implementation would go here
// For now, return informative error
return {
success: false,
error: 'OCR conversion not yet implemented. Use --mode native for text-based PDFs.',
hint: 'Native mode works for most PDFs with selectable text'
};
}
/**
* Check if dependencies are available
* @returns {{native: boolean, ocr: boolean}}
*/
function isAvailable() {
const result = { native: false, ocr: false };
try {
require.resolve('@opendocsg/pdf2md');
result.native = true;
} catch { }
try {
require.resolve('tesseract.js');
require.resolve('pdfjs-dist');
result.ocr = true;
} catch { }
return result;
}
module.exports = { convert, isAvailable };
/**
* Output path resolution for pdf-to-markdown
*/
const fs = require('fs');
const path = require('path');
/**
* Resolve output path from input and user-specified output
* @param {string} inputPath - Absolute path to input file
* @param {string|null} outputPath - User-specified output path (optional)
* @param {string} extension - Output file extension (default: '.md')
* @returns {string} Resolved absolute output path
*/
function resolveOutputPath(inputPath, outputPath, extension = '.md') {
if (!outputPath) {
const dir = path.dirname(inputPath);
const base = path.basename(inputPath, path.extname(inputPath));
return path.join(dir, base + extension);
}
const absoluteOutput = path.resolve(outputPath);
// Check if output is a directory
if (fs.existsSync(absoluteOutput) && fs.statSync(absoluteOutput).isDirectory()) {
const base = path.basename(inputPath, path.extname(inputPath));
return path.join(absoluteOutput, base + extension);
}
// Add extension if missing
if (!absoluteOutput.endsWith(extension)) {
return absoluteOutput + extension;
}
return absoluteOutput;
}
module.exports = { resolveOutputPath };
/**
* PDF type detection - determines if PDF has native text or requires OCR
*/
const fs = require('fs');
/**
* Detect if PDF has extractable native text
* Uses simple heuristic: check if raw PDF contains text streams
*
* @param {string} pdfPath - Path to PDF file
* @returns {Promise<{hasText: boolean, confidence: string}>}
*/
async function detectPdfType(pdfPath) {
try {
// Read first 50KB of PDF to check for text content
const buffer = Buffer.alloc(50000);
const fd = fs.openSync(pdfPath, 'r');
fs.readSync(fd, buffer, 0, 50000, 0);
fs.closeSync(fd);
const content = buffer.toString('latin1');
// Look for text stream markers in PDF
const hasTextStream = content.includes('/Type /Page') &&
(content.includes('BT') || content.includes('/Font'));
// Look for image-only indicators
const hasImages = content.includes('/Image') || content.includes('/XObject');
const hasTextContent = content.includes('Tj') || content.includes('TJ');
if (hasTextContent) {
return { hasText: true, confidence: 'high' };
}
if (hasTextStream && !hasImages) {
return { hasText: true, confidence: 'medium' };
}
if (hasImages && !hasTextStream) {
return { hasText: false, confidence: 'medium' };
}
// Fallback: try to extract text and check length
return { hasText: true, confidence: 'low' };
} catch (error) {
// On error, assume native text
return { hasText: true, confidence: 'low' };
}
}
/**
* Determine conversion mode based on detection and user preference
* @param {string} userMode - User-specified mode: 'auto', 'native', 'ocr'
* @param {{hasText: boolean, confidence: string}} detection - Detection result
* @returns {'native'|'ocr'}
*/
function resolveMode(userMode, detection) {
if (userMode === 'native') return 'native';
if (userMode === 'ocr') return 'ocr';
// Auto mode
return detection.hasText ? 'native' : 'ocr';
}
module.exports = { detectPdfType, resolveMode };
/**
* Tests for pdf-to-markdown converter
*/
const path = require('path');
const fs = require('fs');
const { describe, it, expect } = require('./test-framework.cjs');
const SCRIPTS_DIR = path.join(__dirname, '..', 'scripts', 'lib');
// Test output-handler
describe('output-handler', () => {
const { resolveOutputPath } = require(path.join(SCRIPTS_DIR, 'output-handler.cjs'));
it('should resolve output path from input (no output specified)', () => {
const input = path.join('path', 'to', 'document.pdf');
const result = resolveOutputPath(input, null, '.md');
const expected = path.join('path', 'to', 'document.md');
expect(result).toBe(expected);
});
it('should use explicit output path', () => {
const input = path.join('path', 'to', 'document.pdf');
const output = path.join('other', 'path', 'result.md');
const result = resolveOutputPath(input, output, '.md');
expect(result).toContain('result.md');
});
it('should add .md extension if missing', () => {
const input = path.join('path', 'to', 'document.pdf');
const output = path.join('other', 'path', 'result');
const result = resolveOutputPath(input, output, '.md');
expect(result).toContain('result.md');
});
});
// Test pdf-detector
describe('pdf-detector', () => {
const { resolveMode } = require(path.join(SCRIPTS_DIR, 'pdf-detector.cjs'));
it('should return native for native mode override', () => {
const result = resolveMode('native', { hasText: false, confidence: 'high' });
expect(result).toBe('native');
});
it('should return ocr for ocr mode override', () => {
const result = resolveMode('ocr', { hasText: true, confidence: 'high' });
expect(result).toBe('ocr');
});
it('should return native for auto mode with detected text', () => {
const result = resolveMode('auto', { hasText: true, confidence: 'high' });
expect(result).toBe('native');
});
it('should return ocr for auto mode without detected text', () => {
const result = resolveMode('auto', { hasText: false, confidence: 'high' });
expect(result).toBe('ocr');
});
});
// Test converter availability
describe('converter', () => {
const { isAvailable } = require(path.join(SCRIPTS_DIR, 'converter.cjs'));
it('should return availability object', () => {
const available = isAvailable();
expect(typeof available).toBe('object');
expect(typeof available.native).toBe('boolean');
expect(typeof available.ocr).toBe('boolean');
});
it('should have native or ocr availability', () => {
const available = isAvailable();
// At least one should be determinable
expect(typeof available.native).toBe('boolean');
});
});
// Test converter validation
describe('converter-validation', () => {
const { convert } = require(path.join(SCRIPTS_DIR, 'converter.cjs'));
it('should reject missing input', async () => {
const result = await convert({});
expect(result.success).toBe(false);
expect(result.error).toContain('required');
});
it('should reject non-existent file', async () => {
const result = await convert({ input: '/nonexistent/file.pdf' });
expect(result.success).toBe(false);
expect(result.error).toContain('not found');
});
it('should reject non-PDF file', async () => {
// Create temp file
const tempFile = path.join(__dirname, 'fixtures', 'temp.txt');
fs.mkdirSync(path.dirname(tempFile), { recursive: true });
fs.writeFileSync(tempFile, 'test');
const result = await convert({ input: tempFile });
expect(result.success).toBe(false);
expect(result.error).toContain('.pdf');
// Cleanup
fs.unlinkSync(tempFile);
});
});
test#!/usr/bin/env node
/**
* Test runner for pdf-to-markdown
*/
const path = require('path');
console.log('='.repeat(60));
console.log('pdf-to-markdown Test Suite');
console.log('='.repeat(60) + '\n');
// Run test suites
require('./converter.test.cjs');
// Print summary
const { printSummary } = require('./test-framework.cjs');
const success = printSummary();
process.exit(success ? 0 : 1);
/**
* Minimal test framework for pdf-to-markdown
* Self-contained, no external dependencies
*/
const results = { passed: 0, failed: 0, suites: {} };
function describe(suiteName, fn) {
results.suites[suiteName] = { passed: 0, failed: 0, tests: [] };
global.__currentSuite = suiteName;
fn();
}
function it(testName, fn) {
const suite = results.suites[global.__currentSuite];
try {
fn();
suite.passed++;
results.passed++;
suite.tests.push({ name: testName, passed: true });
console.log(` \x1b[32m✓\x1b[0m ${testName}`);
} catch (error) {
suite.failed++;
results.failed++;
suite.tests.push({ name: testName, passed: false, error: error.message });
console.log(` \x1b[31m✗\x1b[0m ${testName}`);
console.log(` Error: ${error.message}`);
}
}
function expect(actual) {
return {
toBe(expected) {
if (actual !== expected) {
throw new Error(`Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
}
},
toEqual(expected) {
if (JSON.stringify(actual) !== JSON.stringify(expected)) {
throw new Error(`Expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
}
},
toBeTruthy() {
if (!actual) {
throw new Error(`Expected truthy value, got ${JSON.stringify(actual)}`);
}
},
toBeFalsy() {
if (actual) {
throw new Error(`Expected falsy value, got ${JSON.stringify(actual)}`);
}
},
toContain(expected) {
if (typeof actual === 'string') {
if (!actual.includes(expected)) {
throw new Error(`Expected "${actual}" to contain "${expected}"`);
}
} else if (Array.isArray(actual)) {
if (!actual.includes(expected)) {
throw new Error(`Expected array to contain ${JSON.stringify(expected)}`);
}
}
},
toThrow(expectedMessage) {
let threw = false;
let errorMessage = '';
try {
actual();
} catch (e) {
threw = true;
errorMessage = e.message;
}
if (!threw) {
throw new Error('Expected function to throw');
}
if (expectedMessage && !errorMessage.includes(expectedMessage)) {
throw new Error(`Expected error to contain "${expectedMessage}", got "${errorMessage}"`);
}
},
toBeOneOf(expected) {
if (!expected.includes(actual)) {
throw new Error(`Expected ${JSON.stringify(actual)} to be one of ${JSON.stringify(expected)}`);
}
}
};
}
function printSummary() {
console.log('\n' + '='.repeat(60));
console.log('Results');
console.log('='.repeat(60) + '\n');
for (const [suiteName, suite] of Object.entries(results.suites)) {
const status = suite.failed === 0 ? '\x1b[32mPASS\x1b[0m' : '\x1b[31mFAIL\x1b[0m';
console.log(`${status}: ${suiteName} (${suite.passed}/${suite.passed + suite.failed})`);
}
console.log('\n' + '='.repeat(60));
console.log(`Total: ${results.passed + results.failed} | Passed: ${results.passed} | Failed: ${results.failed}`);
console.log('='.repeat(60));
return results.failed === 0;
}
module.exports = { describe, it, expect, printSummary, results };
Related skills
How it compares
Choose pdf-to-markdown when PDF source files must become Markdown for docs or agents, not when you only need quick plain-text copy-paste from a viewer.
FAQ
Does pdf-to-markdown support scanned PDFs?
pdf-to-markdown supports scanned PDFs through OCR alongside native-text extraction. The skill auto-detects whether a PDF has selectable text or needs OCR, then runs the appropriate conversion path for Markdown output.
What does pdf-to-markdown output?
pdf-to-markdown outputs clean Markdown files plus JSON metadata reporting success status, page count, conversion mode, and the output file path. Developers use the Markdown for docs sites, specs, or agent RAG pipelines.
Is Pdf To Markdown safe to install?
skills.sh reports 2 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.