Commit 382928cc9affe927d32785b84f932e539f2712bc
1 parent
ab54ad1e94
table_demo: added table drawing code from PDFlib table demo
Showing 1 changed file with 56 additions and 7 deletions Side-by-side Diff
table_demo
View file @
382928c
... | ... | @@ -30,6 +30,12 @@ |
30 | 30 | $pageTopYPortrait = 750; |
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 | + | |
33 | 39 | // wrap PDFlib usage in a try{} block |
34 | 40 | try { |
35 | 41 | $pdf = new PDFlib(); |
... | ... | @@ -57,7 +63,7 @@ |
57 | 63 | . "encoding=unicode "; |
58 | 64 | |
59 | 65 | // START PAGE 1 |
60 | - $pdf->begin_page_ext(0, 0, 'width=letter.width, height=letter.height'); | |
66 | + $pdf->begin_page_ext(0, 0, 'width=letter.width height=letter.height'); | |
61 | 67 | |
62 | 68 | // create bookmarks first thing |
63 | 69 | $action = $pdf->create_action("GoTo", "destination={page=1}"); |
64 | 70 | |
65 | 71 | |
66 | 72 | |
... | ... | @@ -117,18 +123,61 @@ |
117 | 123 | // END PAGE 2 |
118 | 124 | $pdf->end_page_ext(''); |
119 | 125 | |
120 | - // START PAGE 3 | |
121 | - $pdf->begin_page_ext(0, 0, 'width=letter.width, height=letter.height'); | |
126 | + // SET UP THE TABLE | |
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 | |
123 | - // END PAGE 3 | |
124 | - $pdf->end_page_ext(''); | |
139 | + // DRAW THE TABLE | |
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 | |
166 | + * deal with. | |
167 | + */ | |
168 | + die("User return found in Table"); | |
169 | + } | |
170 | + } | |
171 | + | |
172 | + /* This will also delete Textflow handles used in the table */ | |
173 | + $pdf->delete_table($tbl, ""); | |
174 | + | |
126 | 175 | // close the document |
127 | 176 | $pdf->end_document(''); |
128 | 177 | } |
129 | 178 | |
130 | 179 | catch (PDFlibException $e) { |
131 | - die("PDFlib exception occurred in sample:\n" . | |
180 | + die("PDFlib exception occurred:\n" . | |
132 | 181 | "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . |
133 | 182 | $e->get_errmsg() . "\n"); |
134 | 183 | } |