you need a bold font for such cell. You can see this in the code sample within the PDFlib cookbook: table/starter_table use the bold text cells in for the header descriptions.
/* ---------- row 1: table header (spans all columns) */
$row = 1; $col = 1;
$font = $p->load_font("NotoSerif-Bold", "unicode", "");
if ($font == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
$optlist = "fittextline={position=center font=" . $font . " fontsize=14} " .
"colspan=" . $colmax;
$tbl = $p->add_table_cell($tbl, $col, $row, $headertext, $optlist);
if ($tbl == 0) {
echo("Error: " . $p->get_errmsg());
exit(1);
}
or table/mixed table contents
/* Load the font */
$boldfont = $p->load_font("Helvetica-Bold", "unicode", "");
if ($boldfont == 0)
throw new Exception("Error: " . $p->get_errmsg());
...
/* ---------------------
* Adding the first cell
* ---------------------
*
* The cell will be placed in the first column of the first row and will
* span three columns.
* The first column has a width of 50 points.
* The text line is centered vertically and horizontally, with a margin
* of 4 points from all borders.
*/
$optlist = "fittextline={font=" . $boldfont . " fontsize=12" .
" position=center} margin=4 colspan=3 colwidth=" . $c1;
$tbl = $p->add_table_cell($tbl, 1, 1, "Our Paper Plane Models", $optlist);
You can apply the font handle in the fittextline={} option. Of course you can also do an implicit load_font() by using the option fontname and encoding like:
$optlist = "fittextline={fontname=NotoSerif-Bold encoding=unicode fontsize=12" .
" position=center} margin=4 colspan=3 colwidth=" . $c1;
The starter_table.php sample is also included in the PDFlib 9 download package within the bind/php directory (or any other supported binding)
Answer from Rainer on Stack Overflow_list needs to return a List<List<String>> as it need to return multiple string lists - one for each item. It can be written with List.map() like:
List<List<String>> _list() {
return _items
.map((item) => <String>[
item.title,
'HSN',
item.quantity.toString(),
// etc
])
.toList();
}
which maps each item into a string list, and returns the list of lists.
You need to change how you use that to use a spread operator ... so that the list of items is expanded into the overall list, like this:
var data = <List<String>>[
<String>[
'#',
'Product Name',
'HSN',
'Qty',
'Unit Price',
'MRP',
'Disc%',
'Disc Amnt',
'Taxable Amnt',
'SGST%',
'SGST Amnt',
'CGST%',
'CGST Amnt',
'Net Amnt'
],
..._list(),
];
}
Or, you could inline the whole operation with the for operator:
var data = <List<String>>[
<String>[
'#',
'Product Name',
'HSN',
'Qty',
'Unit Price',
'MRP',
'Disc%',
'Disc Amnt',
'Taxable Amnt',
'SGST%',
'SGST Amnt',
'CGST%',
'CGST Amnt',
'Net Amnt'
],
for (var item in _items)
<String>[
item.title,
'HSN',
item.quantity.toString(),
// etc
],
];
}
There are several ways to do it, I prefer to fill the List separately, like:
`List<List<String>> salidas = new List();
salidas.add(<String>['Title1','Title2', ... , 'Title n']);
for(var indice=0;indice<records.length;indice++) {
var recind = {
'field1': records[indice].Stringfield1,
'field2': records[indice].Stringfield2,
...
'fieldn': records[indice].Stringfieldn
};
salidas.add(recind);
}
...
fpdf.Table.fromTextArray(context: context,data: salidas),
`
I've worked with PDF generation a few times in the past, and generally find it to be a huge pain in the neck.
PDFLib's documentation http://www.pdflib.com/fileadmin/pdflib/pdf/manuals/PDFlib-8.0.2-tutorial.pdf starts explaining what you're looking for in section 8.2, page 193. You'll be creating multi-line flows. The code there looks intimidating, but take some time to work through it, it's pretty close to what you'll end up using.
I may be able to find some code later, but I forget what library I was using. For now a few tips:
- Work it out on paper, just like their marked up examples. Where you want things to start, end, and such.
- Use clear variable names to store those offsets. Not constants!
- Find good extreme examples to test with while developing. Developing with text like "test" to find out later you need to support "I am the very model of the modern major general" may throw off your entire flow, and require you to start from scratch.
- Some libraries "support" HTML embeds, including HTML tables. This siren song is sweet, but will lead you into the razor sharp rocks. Every library I've used supports them a little bit, but then you run into a wall where you can't get the next little tweak without dropping tables and reverting to native functions. They've been a huge waste of time to play with, one and all.
update I've found my most recent code iteration, we used the library from http://www.tcpdf.org. It worked, mostly. I dealt with a lot of inconsistencies in where the cursor was left after writing multiple lines of text to a page. I ended up ripping out anything that used their multi-line code and writing my own. That done it got pretty easy to work with.
Table handling in PDFlib is made extremely difficult. Tables work, but in cases where you have multiple tables in top of each other and want the below one tables to be always at a certain distance of upper table's bottom line or want to use nested tables, you are in trouble. These like behaviors can be made, but the code is complicated. WHY pdflib team didn't take usage behaviour of html tables, where they have worked well two centuries.
Because html tables are easy to use, one good method is to use phantomJS to generate pdf from html. PhantomJS uses webkit for page rendering and supports html5+css3+svg+canvas. And in addition to pdf, it can output png, jpeg and gif.
Here is an example of using phantomJS to generate PDF-invoices: http://we-love-php.blogspot.fi/2012/12/create-pdf-invoices-with-html5-and-phantomjs.html