set_option("searchpath={" . $searchpath . "}"); /* This means we must check return values of load_font() etc. */ $p->set_option("errorpolicy=return"); if ($p->begin_document("", "") == 0) throw new Exception("Error: " . $p->get_errmsg()); $p->set_info("Creator", "PDFlib Cookbook"); $p->set_info("Title", $title); $p->begin_page_ext(0, 0, "width=" . $pagewidth . " height=" . $pageheight); /* Set the current row */ $row = 1; $header_optlist = "fittextline={fontname=NotoSerif-Regular fontsize=" . $fontsize . " position=center} rowheight=" . $rowheight . " margin=" . $margin . " colspan=2 colwidth=" . $tablewidth; /* Add a heading line spanning two columns. */ $tbl = $p->add_table_cell(0, 1, $row, "Decimal alignment with Textlines in table cells", $header_optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $row += 1; /* Format the numbers according to the US locale. */ /* * Add two cells per row for subsequent rows. The first column * contains a consecutive number, the second column an * arbitrary floating point number formatted with two digits * after the decimal point. */ for ($i = 0, $factor = 1; $i < $textline_rowcount; $row += 1, $i += 1, $factor *= 10) { /* Add a cell in the first column of the current row. */ $optlist = "fittextline={fontname=NotoSerif-Regular fontsize=" . $fontsize . " position={center center}} rowheight=" . $rowheight . " margin=" . $margin . " colwidth=" . $c1; $tbl = $p->add_table_cell($tbl, 1, $row, "" . ($i + 1), $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* * Add a cell where the decimal numbers are aligned at the * decimal point. The decimal point is placed at 80% of the cell * width. */ $optlist = "fittextline={fontname=NotoSerif-Regular fontsize=" . $fontsize . " position={80 center} alignchar={.}} colwidth=" . $c2 . " margin=" . $margin; /* Generate a decimal number and put into a table cell */ $number = number_format(1.33 * ($i + 1) * $factor + 1.71 * ($i + 1), 2); $tbl = $p->add_table_cell($tbl, 2, $row, $number, $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); } $tbl = $p->add_table_cell($tbl, 1, $row, "Decimal alignment with Textflows in table cells", $header_optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); $row += 1; for ($i = 0, $factor = 1; $i < $textflow_rowcount; $row += 1, $i += 1, $factor *= 10) { /* Add a cell in the first column of the current row. */ $optlist = "fittextline={fontname=NotoSerif-Regular fontsize=" . $fontsize . " position={center center}} rowheight=" . $rowheight . " margin=" . $margin . " colwidth=" . $c1; $tbl = $p->add_table_cell($tbl, 1, $row, "" . ($i + 1), $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Generate two decimal numbers and per table cell */ $number1 = number_format(1.13 * ($i + 1) * $factor + 1.21 * ($i + 1), 2); $number2 = number_format(1.52 * ($i + 1) * $factor + 1.17 * ($i + 1), 2); /* * Create the text content from the two numbers, separated * by a newline, and aligned with the TAB character. */ $tftext = "\t" . $number1 . "\n\t" . $number2; /* * Prepare the option list for adding the Textflow. * * Set the tab alignment character and set the tab position at * 80% of the formatted Textflow width. We explicitly set the * "tabalignchar" to the default ".", because it potentially * would have to be changed for other locales. */ $optlist = "fontname=NotoSerif-Regular fontsize=" . $fontsize . " hortabmethod=ruler " . "ruler={80%} tabalignment={decimal} tabalignchar={.}"; /* Add the Textflow to be placed in a table cell. */ $tf = $p->add_textflow(0, $tftext, $optlist); if ($tf == 0) throw new Exception("Error: " . $p->get_errmsg()); /* Prepare the option list for adding the Textflow table cell.*/ $optlist = "textflow=" . $tf . " colwidth=" . $c2 . " margin=" . $margin; $tbl = $p->add_table_cell($tbl, 2, $row, "", $optlist); if ($tbl == 0) throw new Exception("Error: " . $p->get_errmsg()); } /* Fit the table centered on the page. */ $optlist = "header=1 stroke={{line=other linewidth=0.3}}"; $llx = ($pagewidth - $tablewidth) / 2; $tableheight = (2 + $textline_rowcount + 2 * $textflow_rowcount) * $rowheight; $lly = ($pageheight - $tableheight) / 2; $result = $p->fit_table($tbl, $llx, $lly, $llx + $tablewidth, $lly + $tableheight, $optlist); /* Check the result; "_stop" means all is ok */ if ($result != "_stop") { if ($result == "_error") throw new Exception("Error: " . $p->get_errmsg()); else { /* * Other return values require dedicated code to deal with */ } } $p->delete_table($tbl, ""); $p->end_page_ext(""); $p->end_document(""); $buf = $p->get_buffer(); $len = strlen($buf); header("Content-type: application/pdf"); header("Content-Length: $len"); header("Content-Disposition: inline; filename=align_decimals.pdf"); print $buf; } catch (PDFlibException $e) { echo("PDFlib exception occurred:\n". "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . $e->get_errmsg() . "\n"); exit(1); } catch (Throwable $e) { echo("PHP exception occurred: " . $e->getMessage() . "\n"); exit(1); } $p = 0; ?>