Commit 382928cc9affe927d32785b84f932e539f2712bc

Authored by Brian Manning
1 parent ab54ad1e94

table_demo: added table drawing code from PDFlib table demo

Showing 1 changed file with 56 additions and 7 deletions Inline Diff

#!/usr/bin/env php 1 1 #!/usr/bin/env php
2 2
<?php 3 3 <?php
/** 4 4 /**
* FILENAME: 5 5 * FILENAME:
* pdflib_demo 6 6 * pdflib_demo
* 7 7 *
* DESCRIPTION: 8 8 * DESCRIPTION:
* Demonstration of the pdflib PHP Library for generating PDF files 9 9 * Demonstration of the pdflib PHP Library for generating PDF files
* 10 10 *
*/ 11 11 */
12 12
/** 13 13 /**
Links: 14 14 Links:
- https://www.pdflib.com/developer/technical-documentation/manuals/ 15 15 - https://www.pdflib.com/developer/technical-documentation/manuals/
- https://www.pdflib.com/pdflib-cookbook/browse-all-topics/ 16 16 - https://www.pdflib.com/pdflib-cookbook/browse-all-topics/
- https://www.pdflib.com/pdflib-cookbook/tables/starter-table/ 17 17 - https://www.pdflib.com/pdflib-cookbook/tables/starter-table/
- https://www.pdflib.com/pdflib-cookbook/tables/vertical-text-alignment/ 18 18 - https://www.pdflib.com/pdflib-cookbook/tables/vertical-text-alignment/
- https://www.pdflib.com/pdflib-cookbook/general-programming/starter-basic/php-general-progamming-issues/ 19 19 - https://www.pdflib.com/pdflib-cookbook/general-programming/starter-basic/php-general-progamming-issues/
20 20
*/ 21 21 */
22 22
// set a default timezone 23 23 // set a default timezone
date_default_timezone_set("America/Los_Angeles"); 24 24 date_default_timezone_set("America/Los_Angeles");
25 25
//require_once('tcpdf_include.php'); 26 26 //require_once('tcpdf_include.php');
$outfile = __DIR__ . '/pdflib_table_output.pdf'; 27 27 $outfile = __DIR__ . '/pdflib_table_output.pdf';
28 28
// page placement defaults 29 29 // page placement defaults
$pageTopYPortrait = 750; 30 30 $pageTopYPortrait = 750;
$pageTopYLandscape = 570; 31 31 $pageTopYLandscape = 570;
32 32
33 // table params
34 $tbl = 0;
35 $rowmax = 50;
36 $colmax = 5;
37 $llx= 50; $lly=50; $urx=550; $ury=800;
38
// wrap PDFlib usage in a try{} block 33 39 // wrap PDFlib usage in a try{} block
try { 34 40 try {
$pdf = new PDFlib(); 35 41 $pdf = new PDFlib();
36 42
// set up some PDFlib options 37 43 // set up some PDFlib options
$pdf->set_option("errorpolicy=return"); 38 44 $pdf->set_option("errorpolicy=return");
$pdf->set_option("stringformat=utf8"); 39 45 $pdf->set_option("stringformat=utf8");
// all paths in 'searchpath' need to be inside curly braces 40 46 // all paths in 'searchpath' need to be inside curly braces
// multiple curly-braced paths in $searchpath need to be separated with 41 47 // multiple curly-braced paths in $searchpath need to be separated with
// space characters 42 48 // space characters
$searchpath = '{/usr/share/fonts/dejavu/}{' . __DIR__ . '/fonts}'; 43 49 $searchpath = '{/usr/share/fonts/dejavu/}{' . __DIR__ . '/fonts}';
$pdf->set_option("searchpath={" . $searchpath . "}"); 44 50 $pdf->set_option("searchpath={" . $searchpath . "}");
45 51
if ($pdf->begin_document($outfile, "") == 0) 46 52 if ($pdf->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $pdf->get_errmsg()); 47 53 throw new Exception("Error: " . $pdf->get_errmsg());
48 54
// set up the document metadata info 49 55 // set up the document metadata info
$pdf->set_info('Creator', 'IODP Science Support Office'); 50 56 $pdf->set_info('Creator', 'IODP Science Support Office');
$pdf->set_info('Author', 'IODP Science Support Office'); 51 57 $pdf->set_info('Author', 'IODP Science Support Office');
$pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals'); 52 58 $pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals');
$pdf->set_info('Subject', 'IODP Proposals'); 53 59 $pdf->set_info('Subject', 'IODP Proposals');
$pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition'); 54 60 $pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition');
55 61
$textOpts = "fontname={FreeSans} embedding fontsize=20 " 56 62 $textOpts = "fontname={FreeSans} embedding fontsize=20 "
. "encoding=unicode "; 57 63 . "encoding=unicode ";
58 64
// START PAGE 1 59 65 // START PAGE 1
$pdf->begin_page_ext(0, 0, 'width=letter.width, height=letter.height'); 60 66 $pdf->begin_page_ext(0, 0, 'width=letter.width height=letter.height');
61 67
// create bookmarks first thing 62 68 // create bookmarks first thing
$action = $pdf->create_action("GoTo", "destination={page=1}"); 63 69 $action = $pdf->create_action("GoTo", "destination={page=1}");
$pdf->create_bookmark("Page 1", " action={activate= " . $action . "}"); 64 70 $pdf->create_bookmark("Page 1", " action={activate= " . $action . "}");
65 71
// action 66 72 // action
$action = $pdf->create_action("GoTo", "destination={page=2}"); 67 73 $action = $pdf->create_action("GoTo", "destination={page=2}");
// bookmark 68 74 // bookmark
$pdf->create_bookmark("Page 2", " action={activate= " . $action . "}"); 69 75 $pdf->create_bookmark("Page 2", " action={activate= " . $action . "}");
70 76
// text/image 71 77 // text/image
$pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, 72 78 $pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750,
$textOpts . " underline=true matchbox={name=to_second_page}"); 73 79 $textOpts . " underline=true matchbox={name=to_second_page}");
// annotation; reuse the "page 2" action above 74 80 // annotation; reuse the "page 2" action above
$optlist = "action={activate " . $action . "} linewidth=0 " . 75 81 $optlist = "action={activate " . $action . "} linewidth=0 " .
"usematchbox={to_second_page}"; 76 82 "usematchbox={to_second_page}";
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); 77 83 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
78 84
// the rest of the text on page 1 79 85 // the rest of the text on page 1
$pdf->fit_textline("This is some text on the first page; ", 20, 720, 80 86 $pdf->fit_textline("This is some text on the first page; ", 20, 720,
$textOpts); 81 87 $textOpts);
$pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts); 82 88 $pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts);
83 89
// END PAGE 1 84 90 // END PAGE 1
$pdf->end_page_ext(''); 85 91 $pdf->end_page_ext('');
86 92
// START PAGE 2 87 93 // START PAGE 2
//$pdf->begin_page_ext(612, 792, ''); 88 94 //$pdf->begin_page_ext(612, 792, '');
$ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", ''); 89 95 $ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", '');
if ($ssfPDF == 0) 90 96 if ($ssfPDF == 0)
throw new Exception("Error: " . $p->get_errmsg()); 91 97 throw new Exception("Error: " . $p->get_errmsg());
92 98
$ssfPage = $pdf->open_pdi_page($ssfPDF, 1, ''); 93 99 $ssfPage = $pdf->open_pdi_page($ssfPDF, 1, '');
$ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width"); 94 100 $ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width");
$ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height"); 95 101 $ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height");
//print "Page width/height: $ssfWidth x $ssfHeight\n"; 96 102 //print "Page width/height: $ssfWidth x $ssfHeight\n";
97 103
$pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); 98 104 $pdf->begin_page_ext($ssfWidth, $ssfHeight, '');
99 105
$pdf->fit_pdi_page($ssfPage, 0, 0, ''); 100 106 $pdf->fit_pdi_page($ssfPage, 0, 0, '');
$pdf->close_pdi_page($ssfPage); 101 107 $pdf->close_pdi_page($ssfPage);
$pdf->close_pdi_document($ssfPDF); 102 108 $pdf->close_pdi_document($ssfPDF);
103 109
// write some text on to the page 104 110 // write some text on to the page
// for landscape mode... 105 111 // for landscape mode...
//$pdf->fit_textline("Back to Top", 20, 570, 106 112 //$pdf->fit_textline("Back to Top", 20, 570,
//$linkOpts = linkAttribs(1, 750); 107 113 //$linkOpts = linkAttribs(1, 750);
$action = $pdf->create_action("GoTo", "destination={page=1}"); 108 114 $action = $pdf->create_action("GoTo", "destination={page=1}");
$pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, 109 115 $pdf->fit_textline("Back to Top", 20, $ssfHeight - 30,
$textOpts . " underline=true matchbox={name=to_first_page}"); 110 116 $textOpts . " underline=true matchbox={name=to_first_page}");
111 117
// call create_annotation here 112 118 // call create_annotation here
$optlist = "action={activate " . $action . "} linewidth=0 " . 113 119 $optlist = "action={activate " . $action . "} linewidth=0 " .
"usematchbox={to_first_page}"; 114 120 "usematchbox={to_first_page}";
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); 115 121 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
116 122
// END PAGE 2 117 123 // END PAGE 2
$pdf->end_page_ext(''); 118 124 $pdf->end_page_ext('');
119 125
// START PAGE 3 120 126 // SET UP THE TABLE
$pdf->begin_page_ext(0, 0, 'width=letter.width, height=letter.height'); 121 127 $font = $pdf->load_font("FreeSans", "unicode", "");
128 for ($row = 1; $row <= $rowmax; $row++) {
129 for ($col = 1; $col <= $colmax; $col++) {
130 $num = "Col " . $col . "/Row " . $row;
131 $optlist = "colwidth=20% fittextline={font=" . $font . " fontsize=10}";
132 $tbl = $pdf->add_table_cell($tbl, $col, $row, $num, $optlist);
133 if ($tbl == 0) {
134 die("Error: " . $pdf->get_errmsg());
135 }
136 }
137 }
122 138
// END PAGE 3 123 139 // DRAW THE TABLE
$pdf->end_page_ext(''); 124 140 do {
141 $pdf->begin_page_ext(0, 0, "width=letter.width height=letter.height");
125 142
143 /* Shade every other $row; draw lines for all table cells.
144 * Add "showcells showborder" to visualize cell borders
145 */
146 $optlist = "header=1 rowheightdefault=auto " .
147 "fill={{area=rowodd fillcolor={gray 0.9}}} " .
148 "stroke={{line=other}} ";
149
150 /* Place the table instance */
151 $result = $pdf->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);
152 if ($result == "_error") {
153 die("Couldn't place table: " . $p->get_errmsg());
154 }
155
156 $pdf->end_page_ext("");
157
158 } while ($result == "_boxfull");
159 /* Check the $result; "_stop" means all is ok. */
160 if ($result != "_stop") {
161 if ($result == "_error") {
162 die("Error when placing table: " . $pdf->get_errmsg());
163 } else {
164 /* Any other return value is a user exit caused by
165 * the "return" option; this requires dedicated code to