Commit 913794bed778e5ebd9fe929add6cba8dc1e02f7c
1 parent
382928cc9a
table_demo: added PDFlib license key to table demo
Showing 1 changed file with 3 additions and 0 deletions Inline Diff
table_demo
View file @
913794b
#!/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 | |||
// table params | 33 | 33 | // table params | |
$tbl = 0; | 34 | 34 | $tbl = 0; | |
$rowmax = 50; | 35 | 35 | $rowmax = 50; | |
$colmax = 5; | 36 | 36 | $colmax = 5; | |
$llx= 50; $lly=50; $urx=550; $ury=800; | 37 | 37 | $llx= 50; $lly=50; $urx=550; $ury=800; | |
38 | 38 | |||
// wrap PDFlib usage in a try{} block | 39 | 39 | // wrap PDFlib usage in a try{} block | |
try { | 40 | 40 | try { | |
$pdf = new PDFlib(); | 41 | 41 | $pdf = new PDFlib(); | |
42 | 42 | |||
// set up some PDFlib options | 43 | 43 | // set up some PDFlib options | |
$pdf->set_option("errorpolicy=return"); | 44 | 44 | $pdf->set_option("errorpolicy=return"); | |
$pdf->set_option("stringformat=utf8"); | 45 | 45 | $pdf->set_option("stringformat=utf8"); | |
46 | // set up the license key; Linux x86_64 | |||
47 | $pdf->set_option("license=L900202-010053-139026-P52782-GB5G52"); | |||
48 | ||||
// all paths in 'searchpath' need to be inside curly braces | 46 | 49 | // all paths in 'searchpath' need to be inside curly braces | |
// multiple curly-braced paths in $searchpath need to be separated with | 47 | 50 | // multiple curly-braced paths in $searchpath need to be separated with | |
// space characters | 48 | 51 | // space characters | |
$searchpath = '{/usr/share/fonts/dejavu/}{' . __DIR__ . '/fonts}'; | 49 | 52 | $searchpath = '{/usr/share/fonts/dejavu/}{' . __DIR__ . '/fonts}'; | |
$pdf->set_option("searchpath={" . $searchpath . "}"); | 50 | 53 | $pdf->set_option("searchpath={" . $searchpath . "}"); | |
51 | 54 | |||
if ($pdf->begin_document($outfile, "") == 0) | 52 | 55 | if ($pdf->begin_document($outfile, "") == 0) | |
throw new Exception("Error: " . $pdf->get_errmsg()); | 53 | 56 | throw new Exception("Error: " . $pdf->get_errmsg()); | |
54 | 57 | |||
// set up the document metadata info | 55 | 58 | // set up the document metadata info | |
$pdf->set_info('Creator', 'IODP Science Support Office'); | 56 | 59 | $pdf->set_info('Creator', 'IODP Science Support Office'); | |
$pdf->set_info('Author', 'IODP Science Support Office'); | 57 | 60 | $pdf->set_info('Author', 'IODP Science Support Office'); | |
$pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals'); | 58 | 61 | $pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals'); | |
$pdf->set_info('Subject', 'IODP Proposals'); | 59 | 62 | $pdf->set_info('Subject', 'IODP Proposals'); | |
$pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition'); | 60 | 63 | $pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition'); | |
61 | 64 | |||
$textOpts = "fontname={FreeSans} embedding fontsize=20 " | 62 | 65 | $textOpts = "fontname={FreeSans} embedding fontsize=20 " | |
. "encoding=unicode "; | 63 | 66 | . "encoding=unicode "; | |
64 | 67 | |||
// START PAGE 1 | 65 | 68 | // START PAGE 1 | |
$pdf->begin_page_ext(0, 0, 'width=letter.width height=letter.height'); | 66 | 69 | $pdf->begin_page_ext(0, 0, 'width=letter.width height=letter.height'); | |
67 | 70 | |||
// create bookmarks first thing | 68 | 71 | // create bookmarks first thing | |
$action = $pdf->create_action("GoTo", "destination={page=1}"); | 69 | 72 | $action = $pdf->create_action("GoTo", "destination={page=1}"); | |
$pdf->create_bookmark("Page 1", " action={activate= " . $action . "}"); | 70 | 73 | $pdf->create_bookmark("Page 1", " action={activate= " . $action . "}"); | |
71 | 74 | |||
// action | 72 | 75 | // action | |
$action = $pdf->create_action("GoTo", "destination={page=2}"); | 73 | 76 | $action = $pdf->create_action("GoTo", "destination={page=2}"); | |
// bookmark | 74 | 77 | // bookmark | |
$pdf->create_bookmark("Page 2", " action={activate= " . $action . "}"); | 75 | 78 | $pdf->create_bookmark("Page 2", " action={activate= " . $action . "}"); | |
76 | 79 | |||
// text/image | 77 | 80 | // text/image | |
$pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, | 78 | 81 | $pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, | |
$textOpts . " underline=true matchbox={name=to_second_page}"); | 79 | 82 | $textOpts . " underline=true matchbox={name=to_second_page}"); | |
// annotation; reuse the "page 2" action above | 80 | 83 | // annotation; reuse the "page 2" action above | |
$optlist = "action={activate " . $action . "} linewidth=0 " . | 81 | 84 | $optlist = "action={activate " . $action . "} linewidth=0 " . | |
"usematchbox={to_second_page}"; | 82 | 85 | "usematchbox={to_second_page}"; | |
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); | 83 | 86 | $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); | |
84 | 87 | |||
// the rest of the text on page 1 | 85 | 88 | // the rest of the text on page 1 | |
$pdf->fit_textline("This is some text on the first page; ", 20, 720, | 86 | 89 | $pdf->fit_textline("This is some text on the first page; ", 20, 720, | |
$textOpts); | 87 | 90 | $textOpts); | |
$pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts); | 88 | 91 | $pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts); | |
89 | 92 | |||
// END PAGE 1 | 90 | 93 | // END PAGE 1 | |
$pdf->end_page_ext(''); | 91 | 94 | $pdf->end_page_ext(''); | |
92 | 95 | |||
// START PAGE 2 | 93 | 96 | // START PAGE 2 | |
//$pdf->begin_page_ext(612, 792, ''); | 94 | 97 | //$pdf->begin_page_ext(612, 792, ''); | |
$ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", ''); | 95 | 98 | $ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", ''); | |
if ($ssfPDF == 0) | 96 | 99 | if ($ssfPDF == 0) | |
throw new Exception("Error: " . $p->get_errmsg()); | 97 | 100 | throw new Exception("Error: " . $p->get_errmsg()); | |
98 | 101 | |||
$ssfPage = $pdf->open_pdi_page($ssfPDF, 1, ''); | 99 | 102 | $ssfPage = $pdf->open_pdi_page($ssfPDF, 1, ''); | |
$ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width"); | 100 | 103 | $ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width"); | |
$ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height"); | 101 | 104 | $ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height"); | |
//print "Page width/height: $ssfWidth x $ssfHeight\n"; | 102 | 105 | //print "Page width/height: $ssfWidth x $ssfHeight\n"; | |
103 | 106 | |||
$pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); | 104 | 107 | $pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); | |
105 | 108 | |||
$pdf->fit_pdi_page($ssfPage, 0, 0, ''); | 106 | 109 | $pdf->fit_pdi_page($ssfPage, 0, 0, ''); | |
$pdf->close_pdi_page($ssfPage); | 107 | 110 | $pdf->close_pdi_page($ssfPage); | |
$pdf->close_pdi_document($ssfPDF); | 108 | 111 | $pdf->close_pdi_document($ssfPDF); | |
109 | 112 | |||
// write some text on to the page | 110 | 113 | // write some text on to the page | |
// for landscape mode... | 111 | 114 | // for landscape mode... | |
//$pdf->fit_textline("Back to Top", 20, 570, | 112 | 115 | //$pdf->fit_textline("Back to Top", 20, 570, | |
//$linkOpts = linkAttribs(1, 750); | 113 | 116 | //$linkOpts = linkAttribs(1, 750); | |
$action = $pdf->create_action("GoTo", "destination={page=1}"); | 114 | 117 | $action = $pdf->create_action("GoTo", "destination={page=1}"); | |
$pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, | 115 | 118 | $pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, | |
$textOpts . " underline=true matchbox={name=to_first_page}"); | 116 | 119 | $textOpts . " underline=true matchbox={name=to_first_page}"); | |
117 | 120 | |||
// call create_annotation here | 118 | 121 | // call create_annotation here | |
$optlist = "action={activate " . $action . "} linewidth=0 " . | 119 | 122 | $optlist = "action={activate " . $action . "} linewidth=0 " . | |
"usematchbox={to_first_page}"; | 120 | 123 | "usematchbox={to_first_page}"; | |
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); | 121 | 124 | $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); | |
122 | 125 | |||
// END PAGE 2 | 123 | 126 | // END PAGE 2 | |
$pdf->end_page_ext(''); | 124 | 127 | $pdf->end_page_ext(''); | |
125 | 128 | |||
// SET UP THE TABLE | 126 | 129 | // SET UP THE TABLE | |
$font = $pdf->load_font("FreeSans", "unicode", ""); | 127 | 130 | $font = $pdf->load_font("FreeSans", "unicode", ""); | |
for ($row = 1; $row <= $rowmax; $row++) { | 128 | 131 | for ($row = 1; $row <= $rowmax; $row++) { | |
for ($col = 1; $col <= $colmax; $col++) { | 129 | 132 | for ($col = 1; $col <= $colmax; $col++) { | |
$num = "Col " . $col . "/Row " . $row; | 130 | 133 | $num = "Col " . $col . "/Row " . $row; | |
$optlist = "colwidth=20% fittextline={font=" . $font . " fontsize=10}"; | 131 | 134 | $optlist = "colwidth=20% fittextline={font=" . $font . " fontsize=10}"; | |
$tbl = $pdf->add_table_cell($tbl, $col, $row, $num, $optlist); | 132 | 135 | $tbl = $pdf->add_table_cell($tbl, $col, $row, $num, $optlist); | |
if ($tbl == 0) { | 133 | 136 | if ($tbl == 0) { | |
die("Error: " . $pdf->get_errmsg()); | 134 | 137 | die("Error: " . $pdf->get_errmsg()); | |
} | 135 | 138 | } | |
} | 136 | 139 | } | |
} | 137 | 140 | } | |
138 | 141 | |||
// DRAW THE TABLE | 139 | 142 | // DRAW THE TABLE | |
do { | 140 | 143 | do { | |
$pdf->begin_page_ext(0, 0, "width=letter.width height=letter.height"); | 141 | 144 | $pdf->begin_page_ext(0, 0, "width=letter.width height=letter.height"); | |
142 | 145 | |||
/* Shade every other $row; draw lines for all table cells. | 143 | 146 | /* Shade every other $row; draw lines for all table cells. | |
* Add "showcells showborder" to visualize cell borders | 144 | 147 | * Add "showcells showborder" to visualize cell borders | |
*/ | 145 | 148 | */ | |
$optlist = "header=1 rowheightdefault=auto " . | 146 | 149 | $optlist = "header=1 rowheightdefault=auto " . | |
"fill={{area=rowodd fillcolor={gray 0.9}}} " . | 147 | 150 | "fill={{area=rowodd fillcolor={gray 0.9}}} " . | |
"stroke={{line=other}} "; | 148 | 151 | "stroke={{line=other}} "; | |
149 | 152 | |||
/* Place the table instance */ | 150 | 153 | /* Place the table instance */ | |
$result = $pdf->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist); | 151 | 154 | $result = $pdf->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist); | |
if ($result == "_error") { | 152 | 155 | if ($result == "_error") { | |
die("Couldn't place table: " . $p->get_errmsg()); | 153 | 156 | die("Couldn't place table: " . $p->get_errmsg()); |