I have a large data set which I need to export as a table to a PDF file. At the moment, I do this by exporting as CSV, applying some styling in Excel (display as table, autofit column width, etc.), and then exporting as PDF. I would like to be able to do it in one step directly from Mathematica.
NotebookPrint
combined with the new (v10.3) function TextGrid
seems to be able to present the data quite nicely, but the presentation is poor is when the table spans multiple pages. Is there any way to convince Mathematica to repeat column headings on each page, or to determine where page breaks will occur, so that the repeated headings can be inserted?
For example, here's some data:
data = Select[ExampleData[{"Dataset", "Titanic"}] // Normal,
NumericQ[#age] &][[;; 100]];
Extract the keys and put the data into a list of lists:
headings = Keys[First[data]];
dataAsTable = Outer[Lookup[#1, #2, ""] &, data, headings, 1];
I have tried the following two, which (unlike Export
) produce a PDF document with sensible page sizes and with the table split between pages:
NotebookPrint[
TableForm[dataAsTable, TableHeadings -> {None, headings}] //
OutputForm, "temp.pdf"];
NotebookPrint[
TextGrid[Prepend[dataAsTable, headings], Frame -> All,
Background -> {None, {LightGray, {None}}}], "temp.pdf"];
But in both cases, the way the grid is split at the page break is quite ugly, and there seems to be no way to repeat the headers on each page.
<thead>
elements so that headers are repeated) and then to print the HTML to PDF, for example using a headless Chrome instance. I will write a proper answer when I have implemented this. $\endgroup$