A table and heme can be serialized to a JSON structure in a string or file and then read back. The serialization is a complete representation of the table and the theme. However, there is one crucial caveat. If any callback functions is used in the table they can not be serialized since they are represented by the address to the specific function.
However, this is a situation where a dynamic callback function can be used. They way this works is that the callback function is specified by name with one if the dynamic callback functions (e.g. hpdftbl_set_content_dyncb() ). Such a callback will then be serialized as the name of the function.
When the table is de-serialized back into a table using the function hpdftbl_load() the functions must be available in some linked images to be resolved.
Please note that the table needs to be stroked (via for example hpdftbl_stroke()) before it can be serialized since a number of calculations of internal positions are not calculated until one of the stroke functions are called.
- Note
- Some discussion can be held whether Yaml or Json should be used as serializing format. The reason for choosing Json was primary based on a) easier to manually manipulate a file where invisible spaces doesn't have grammatical meaning. b) existence of efficient libraries
Serializing a table to file
After the table have been stroked it can be saved to a file as the following snippet shows.
int hpdftbl_stroke(HPDF_Doc pdf, const HPDF_Page page, hpdftbl_t t, const HPDF_REAL xpos, const HPDF_REAL ypos, const HPDF_REAL width, HPDF_REAL height)
Stroke the table.
Definition: hpdftbl.c:1682
int hpdftbl_dump(hpdftbl_t tbl, char *filename)
Serialize a table structure as a JSON file.
Definition: hpdftbl_dump.c:190
The snippet above will write a JSON representation of the table to the file table_serialized.json
. The full path is given and it is an error if some intermediate directory does not exist.
An example of a json file can be found here: tut_ex40.json
Serializing a table to a string buffer
This is done with the hpdftbl_dumps() function as the following example shows.
const size_t buffsize=100*1024;
char *sbuff=calloc(buffsize, sizeof(char));
fprintf(stdout,"%s\n",sbuff);
free(sbuff);
int hpdftbl_dumps(hpdftbl_t tbl, char *buff, size_t buffsize)
Serialize a table structure to a string buffer.
Definition: hpdftbl_dump.c:218
Reading back a serialized table
The following snippet shows how the previously serialzed table can be read back and stroked to a PDF file
}
int hpdftbl_stroke_pos(HPDF_Doc pdf, const HPDF_Page page, hpdftbl_t t)
Stroke the table using the already specified size and position within the table.
Definition: hpdftbl.c:1654
int hpdftbl_load(hpdftbl_t tbl, char *filename)
Import a table structure from a serialized table on file.
Definition: hpdftbl_load.c:330
It can be noted that we use the alternative stroke function hpdftbl_stroke_pos() which is used to stroke a table when the position is set within the table itself and don't need to be specified as arguments to the stroke functio as is necessary with hpdftbl_stroke().
- Note
- An error check should always be performed when reading back a table since it is possible that the data have been corrupted.
Serializing a theme to a file
A theme can be serialized with the help of hpdftbl_theme_dump() as the following example that serializes the default theme
int hpdftbl_theme_dump(hpdftbl_theme_t *theme, char *filename)
Serialize the specified theme structure to a named file.
Definition: hpdftbl_dump.c:108
hpdftbl_theme_t * hpdftbl_get_default_theme(void)
Return the default theme.
Definition: hpdftbl_theme.c:259
Define a set of styles into a table theme.
Definition: hpdftbl.h:635
Serializing a theme to a string buffer
This is done with the hpdftbl_theme_dumps() function as the following example shows
const size_t buffsize=2*1024;
char *sbuff=calloc(buffsize, sizeof(char));
fprintf(stdout,"%s\n",sbuff);
free(sbuff);
int hpdftbl_theme_dumps(hpdftbl_theme_t *theme, char *buff, size_t buffsize)
Serialize theme structure to a string buffer.
Definition: hpdftbl_dump.c:135
Reading back a serialized theme
A theme can be read back with the hpdftbl_theme_load() and hpdftbl_theme_loads() functions.
Example of reading back serialized theme and table
The following shows an example of creating a table with a theme where both theme and table are read back from previous serialized representation.
} else {
fprintf(stderr,"%s\n","Failed to load 'tests/default_theme.json'\n");
exit(1);
}
} else {
fprintf(stderr,"%s\n","Failed to load 'tests/tut_ex41.json'\n");
exit(1);
}
int hpdftbl_apply_theme(hpdftbl_t t, hpdftbl_theme_t *theme)
Apply a specified theme to a table.
Definition: hpdftbl_theme.c:177
int hpdftbl_theme_load(hpdftbl_theme_t *tbl, char *filename)
Read a theme from a previous serialized theme from a named file.
Definition: hpdftbl_load.c:282
Core table handle.
Definition: hpdftbl.h:470