Commit f75a06930f185e12cc81895c310922c3b7e24907

Authored by Brian Manning
1 parent aa058d0073
Exists in master

table_demo: fixed table to fit on Letter sized paper

- Added a bunch of defines that represent paper sizes

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