mygems

How to convert a CSV to PDF

A CSV has no page and a PDF is nothing but pages, which is why columns fall off the edge. Here is how to decide what goes in before you convert.

By uos ·

The reason this is annoying is that a CSV has no page. It is an unbounded grid; a PDF is a stack of fixed rectangles. Every conversion has to decide where to cut, and every tool that does it badly makes the same decision: cut wherever the paper runs out, then let the remaining columns fall off the right-hand edge.

So the useful question is not "how do I convert", since almost anything will produce a file, but which columns and which rows are actually going into the PDF. Answer that first and the conversion is trivial.

Before converting: cut the file down

A PDF of 40,000 rows is not a document, it is a mistake with a file size. Nobody opens page 612. If the recipient needs every row, send them the CSV.

A PDF is the right format when the output is meant to be read rather than processed, which means it should be:

  • Filtered. The rows relevant to this recipient, not the whole export.
  • Narrowed. Six to ten columns fit on a landscape A4 page legibly. Fifteen do not, at any font size a person will read.
  • Summarised, often. A GROUP BY that turns 40,000 rows into 30 is usually the document that was actually wanted.

Doing this before the conversion is the entire difference between a usable report and 600 pages of truncated grid.

Orientation and column width

Two settings decide whether the result is legible, and both default wrong.

Landscape, always, unless the file has fewer than five columns. Portrait A4 fits roughly six narrow columns; landscape fits ten.

Scale to fit width, not to a percentage. Every spreadsheet has this and almost nobody finds it: in Excel it is Page Layout → Scale to Fit → Width: 1 page. Without it, columns eleven onward are printed on their own set of pages after the last row, which is how you end up with a 40-page PDF of a 12-page table.

Then two things worth setting once:

  • Repeat the header row on every page. Excel calls it Print Titles → Rows to repeat at top. A table whose header appears only on page one is unreadable from page two.
  • Turn on gridlines. Off by default when printing, and a wide table without them is very hard to read across.

The routes, by situation

From a spreadsheet, on a desktop

Open the CSV, apply the settings above, then Print → Save as PDF. This is the best-looking output of any method here, and the only one where you can put a title and a page number on it without writing code.

Watch the import, though: opening a CSV in Excel converts anything that looks like a date, and a PDF is a permanent record of that mistake. A column of order IDs that arrives as 43831 was 2020-01-01 in the file. Import as text if the data has IDs, leading zeros, or long numbers in it.

From the command line

For a plain, correct table with no styling decisions to make:

csvlook data.csv | enscript -B -f Courier8 -o - | ps2pdf - out.pdf

More practically, if you want control over the layout, go through HTML. Every system has a headless browser and browsers are excellent at paginating tables:

csvtomd data.csv > table.md
pandoc table.md -o out.pdf

The advantage of the HTML route is that @page { size: A4 landscape } and thead { display: table-header-group } in a stylesheet give you exactly the two things spreadsheets bury in dialogs: orientation and repeated headers.

From a phone

This is where the conversion actually comes up: the file is in your email, the person who needs it wants something they can read on a screen without a spreadsheet app, and you are not near a laptop.

The important part is the same as everywhere else: filter and narrow before exporting. A phone-sized PDF with five columns and forty rows is a good deliverable. A phone-sized PDF of the whole file is not.

CSV File Viewer - Smart CSViPhone & iPad · Android

Converts to PDF directly, with the export's columns and styling chosen rather than inherited, and the visual filter editor or a SQL query narrows the file to the rows worth printing first, which is the step that decides whether the PDF is readable.

What it does

When a PDF is the wrong answer

Three cases, and they cover most of the requests.

"So they can open it without Excel." They can already: a CSV opens in Notepad, TextEdit, any browser, Google Sheets, Numbers, and every phone. If the worry is that it will look bad, the fix is usually a narrower file, not a different format.

"So they can't change it." A PDF is not read-only in any meaningful sense. It is trivially editable and trivially extractable. If the requirement is integrity, the answer is a signature or a checksum, not a file format.

"So they can use the data." A PDF is the worst possible container for tabular data, and getting it back out is a task with a whole industry attached. If the recipient will do anything with the numbers, send the CSV and a PDF summary alongside it.

The genuine cases for PDF: something to be printed, something to be archived with fixed pagination, something to be attached to a formal record, or something whose layout must survive being forwarded.

Making it look like a report

Four things, in order of how much they improve the result:

  1. Right-align numbers, left-align text. Nothing else changes legibility as much, and it is one setting.
  2. Fix the decimal places. A column that mixes 1.5 and 1.4999999 reads as sloppy and is usually a float artefact rather than real precision.
  3. Add a header and a footer. Title, the date the data covers (not the date you printed it), and page numbers.
  4. Say what the file is. One line at the top: the source, the filter that was applied, the row count. Six months later this is the difference between a document and a mystery.
CSV Editor - Smart CSViPhone & iPad

Where the file needs fixing before it goes out, whether that is a rounded column, a computed total or deleted rows, the formula columns handle it on the phone, and the result saves inside the CSV so the numbers are the same in whatever prints it.

What it does

Charts instead of tables

Worth asking before converting anything: does the recipient want the rows, or the shape? A trend across twelve months is a line, and a table of twelve months is a worse line that takes longer to read.

The strongest version of this document is usually a chart on the first page and the summarised table behind it, which is a two-minute job in any tool that can produce both, and considerably more useful than either alone.

Questions

How do I convert a CSV file to PDF?
Open it in a spreadsheet, set landscape orientation and 'scale to fit width: 1 page', turn on repeated header rows and gridlines, then print to PDF. On the command line, convert to HTML or Markdown and let a browser or pandoc paginate it. On a phone, an app that exports PDF directly does it in a couple of taps. In all three, filtering the file down first is what decides whether the result is readable.
Why are my columns cut off in the PDF?
Because the print scale defaults to 100%, so anything past the page width is printed on a separate set of pages after the last row instead of being shrunk to fit. Set 'scale to fit width to 1 page'; in Excel it is under Page Layout → Scale to Fit. If the table still cannot be read at that size, the file has too many columns for a page and needs narrowing rather than shrinking.
How do I repeat the header row on every page of the PDF?
In Excel it is Page Layout → Print Titles → 'Rows to repeat at top', set to the header row. In Google Sheets it is Print → Headers and footers → Repeat frozen rows. In an HTML-based conversion, putting the header in a `thead` and setting `display: table-header-group` does the same thing.
Should I send a PDF or the CSV?
Send the CSV if the recipient will do anything with the numbers, because a PDF is the hardest container to get tabular data back out of. Send a PDF when the output is meant to be read, printed, or archived with fixed pagination. A PDF does not make data uneditable, so it is not the answer if the concern is integrity.
How many rows should go in a CSV-to-PDF export?
As few as answer the question. A PDF of tens of thousands of rows is not a document that anyone reads past the first page. Filter to the relevant rows, or summarise with a GROUP BY. Thirty summary rows and a chart are usually what was actually wanted, and the whole file is a better attachment as the original CSV.
Why did my dates and IDs change in the PDF?
The spreadsheet converted them on import, before the PDF existed. Order IDs become serial dates, long numbers become scientific notation, and leading zeros vanish. Import the CSV as text rather than letting the columns be auto-detected, or convert with a tool that does not reinterpret values. The PDF records whatever mistake happened upstream, permanently.

uos Builds Smart CSV Viewer, which exports PDFs, so the question of what should and shouldn't be in one comes up regularly.