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