pdf-to-csv
PDF to CSV
Pull the tables out of a PDF as comma-separated values, one file per table.
- Reads
- a PDF — application/pdf
- Writes
- any number of files — CSV
- Tool page
- /pdf-to-csv
Example
Get the rows of a printed table into a spreadsheet.
Given
- accounts.pdf — a PDF holding a ruled table
From code
import { loadDocument, runOperation } from '@pagecraft/core';
const accounts = await loadDocument({ bytes, name: 'accounts.pdf' });
const [output] = await runOperation('pdf-to-csv', [accounts], {
pages: 'all',
});
From a shell
pagecraft pdf-to-csv accounts.pdf --pages all
What comes back
One CSV file, containing “Region”.
This example is run by the test suite. If it stopped being true, the build would fail.
Options
pages--pages
string, a page selection: '1-3, 7, 9-', 'even', 'last'
Pages
Which pages to take the tables from.
Default: 'all'
bom--bom
boolean
Mark it as UTF-8
Start the file with a byte order mark, without which Excel mangles every accent.
Default: true
What it cannot do
- A PDF does not record what was a table; it records text in places. A table is found by the lines drawn around it, or by its columns lining up, and a table that has neither is read as ordinary text.
- CSV has no way to say that two cells are one, so a merged cell arrives as its text in the place it starts and empty cells where it reached. Use PDF to Excel to keep the merge itself.
- One table becomes one file. A table split across two pages becomes two files, because nothing in the file says the second is a continuation of the first.
- A scanned page holds a picture of a table. Run OCR over it first.