In this lesson you will turn a PDF into text with the capdag command: first
with one capability, then with a machine of two steps that capdag plans for you.
At the end you will stop a run part way through and see what it keeps.
You need capdag installed (Installing the CapDAG CLI)
and a PDF of a few pages. The lesson calls it report.pdf; use any PDF you have,
and type its name where the lesson types report.pdf.
Make a folder for the lesson and put the PDF in it:
mkdir capdag-lesson
cd capdag-lesson
cp ~/Downloads/report.pdf .
Find a capability
A capability (a cap) is one thing capdag can do to a value: split a PDF into pages, render a page as an image, summarize text. Ask for the ones that have to do with PDF:
capdag caps pdf
You see a list. Each line starts with the cap’s name — its alias, the name you type — then its title, then what it takes and what it gives:
disbind-pdf Disbind PDF Into Pages (pdf → page-text)
pdf2png Render PDF Page Images (pdf → png)
…
disbind-pdf takes a PDF and gives the text of its pages. Ask about it:
capdag info disbind-pdf
capdag shows what it takes, the options it has (--index-range, which pages to
take), what it gives, which cartridge provides it, and an example.
Run it on your PDF
capdag run disbind-pdf report.pdf -o pages
While it runs, a block under the command shows the step and its progress. When it ends, the block is replaced by one line that says how the run went, followed by the files it wrote:
✓ report.pdf 1 step · 0.4 s
→ pages/report.output.0.txt
→ pages/report.output.1.txt
Look at one of them:
cat pages/report.output.0.txt
That is the text of the first page. The cap’s own option takes only some pages:
capdag run disbind-pdf report.pdf --index-range 1-2 -o first-pages
Let capdag plan a machine
A machine runs several caps one after another. You do not have to know which caps fit together: ask what your PDF can become.
capdag plan report.pdf
capdag lists what it can make of a PDF: txt, png, and more. On a terminal,
the list is a picker: type to filter it, move with ↑ and ↓, and press Enter to
choose txt. capdag then lists the machines that make text of a PDF, each
written with the names of its steps:
[(pdf) -> disbind -> (page-text)]
[(page-text) -> save-as-txt -> (txt)]
Choose the first machine, then choose save. capdag writes the machine to a file and tells you its name and how to run it.
The same choices, typed as flags, make the same file without asking. This is how you write it in a script:
capdag plan report.pdf --to txt --pick 1 --save pages.machine
Look at what you saved, and let capdag check it:
cat pages.machine
capdag check pages.machine
check says the machine’s input (pdf), its output (txt), and how to run it.
Run the machine
capdag run pages.machine report.pdf -o text
This time the block under the command has two rows: disbind, and the map that
runs save-as-txt on each page, counting the pages as they finish. The run
ends with its summary and the files in text/.
Stop a run
Run the machine on many PDFs at once: copy yours a few times, and give the folder to the machine.
mkdir many
for i in 1 2 3 4 5 6 7 8; do cp report.pdf many/report-$i.pdf; done
capdag run pages.machine many -o many-text
capdag runs the machine once per file. While it is running, press Ctrl-C
once. capdag says it is stopping: nothing new starts, and what started finishes
and is written. The summary names what finished and what never started, and
capdag exits with status 130, as a command a person interrupted does.
Look in many-text: every file a finished run wrote is there, complete.
Had you pressed Ctrl-C a second time, capdag would have aborted instead: everything running is cancelled, and nothing more is written.
What you did
You found a capability by what it works on, ran it on a file, had capdag plan a machine of two steps, saved and checked it, ran it, and stopped a run of it while keeping what it had finished.
From here:
- Every command and flag: CapDAG CLI reference.
- What the terminal shows, and its keys: The terminal interface.
- When a command fails: When a capdag command fails.
- How machines are written: Machine notation.