Commit ab54ad1e9404603e9cc9d2a12d4c5b8f0e452ada

Authored by Brian Manning
1 parent 3c0f907a9c

table_demo: use PDFlib constant definitions when creating new pages

- PDFlib lets you use "constants" for certain values, like page
  height/width for "Letter" size pages

Showing 1 changed file with 10 additions and 4 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
// wrap PDFlib usage in a try{} block 33 33 // wrap PDFlib usage in a try{} block
try { 34 34 try {
$pdf = new PDFlib(); 35 35 $pdf = new PDFlib();
36 36
// set up some PDFlib options 37 37 // set up some PDFlib options
$pdf->set_option("errorpolicy=return"); 38 38 $pdf->set_option("errorpolicy=return");
$pdf->set_option("stringformat=utf8"); 39 39 $pdf->set_option("stringformat=utf8");
// all paths in 'searchpath' need to be inside curly braces 40 40 // all paths in 'searchpath' need to be inside curly braces
// multiple curly-braced paths in $searchpath need to be separated with 41 41 // multiple curly-braced paths in $searchpath need to be separated with
// space characters 42 42 // space characters
$searchpath = '{/usr/share/fonts/dejavu/}{' . __DIR__ . '/fonts}'; 43 43 $searchpath = '{/usr/share/fonts/dejavu/}{' . __DIR__ . '/fonts}';
$pdf->set_option("searchpath={" . $searchpath . "}"); 44 44 $pdf->set_option("searchpath={" . $searchpath . "}");
45 45
if ($pdf->begin_document($outfile, "") == 0) 46 46 if ($pdf->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $pdf->get_errmsg()); 47 47 throw new Exception("Error: " . $pdf->get_errmsg());
48 48
// set up the document metadata info 49 49 // set up the document metadata info
$pdf->set_info('Creator', 'IODP Science Support Office'); 50 50 $pdf->set_info('Creator', 'IODP Science Support Office');
$pdf->set_info('Author', 'IODP Science Support Office'); 51 51 $pdf->set_info('Author', 'IODP Science Support Office');
$pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals'); 52 52 $pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals');
$pdf->set_info('Subject', 'IODP Proposals'); 53 53 $pdf->set_info('Subject', 'IODP Proposals');
$pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition'); 54 54 $pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition');
55 55
$textOpts = "fontname={FreeSans} embedding fontsize=20 " 56 56 $textOpts = "fontname={FreeSans} embedding fontsize=20 "
. "encoding=unicode "; 57 57 . "encoding=unicode ";
58 58
// START PAGE 1 59 59 // START PAGE 1
$pdf->begin_page_ext(612, 792, ''); 60 60 $pdf->begin_page_ext(0, 0, 'width=letter.width, height=letter.height');
61 61
// create bookmarks first thing 62 62 // create bookmarks first thing
$action = $pdf->create_action("GoTo", "destination={page=1}"); 63 63 $action = $pdf->create_action("GoTo", "destination={page=1}");
$pdf->create_bookmark("Page 1", " action={activate= " . $action . "}"); 64 64 $pdf->create_bookmark("Page 1", " action={activate= " . $action . "}");
65 65
// action 66 66 // action
$action = $pdf->create_action("GoTo", "destination={page=2}"); 67 67 $action = $pdf->create_action("GoTo", "destination={page=2}");
// bookmark 68 68 // bookmark
$pdf->create_bookmark("Page 2", " action={activate= " . $action . "}"); 69 69 $pdf->create_bookmark("Page 2", " action={activate= " . $action . "}");
70 70
// text/image 71 71 // text/image
$pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, 72 72 $pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750,
$textOpts . " underline=true matchbox={name=to_second_page}"); 73 73 $textOpts . " underline=true matchbox={name=to_second_page}");
// annotation; reuse the "page 2" action above 74 74 // annotation; reuse the "page 2" action above
$optlist = "action={activate " . $action . "} linewidth=0 " . 75 75 $optlist = "action={activate " . $action . "} linewidth=0 " .
"usematchbox={to_second_page}"; 76 76 "usematchbox={to_second_page}";
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); 77 77 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
78 78
// the rest of the text on page 1 79 79 // the rest of the text on page 1
$pdf->fit_textline("This is some text on the first page; ", 20, 720, 80 80 $pdf->fit_textline("This is some text on the first page; ", 20, 720,
$textOpts); 81 81 $textOpts);
$pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts); 82 82 $pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts);
83 83
// END PAGE 1 84 84 // END PAGE 1
$pdf->end_page_ext(''); 85 85 $pdf->end_page_ext('');
86 86
// START PAGE 2 87 87 // START PAGE 2
//$pdf->begin_page_ext(612, 792, ''); 88 88 //$pdf->begin_page_ext(612, 792, '');
$ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", ''); 89 89 $ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", '');
if ($ssfPDF == 0) 90 90 if ($ssfPDF == 0)
throw new Exception("Error: " . $p->get_errmsg()); 91 91 throw new Exception("Error: " . $p->get_errmsg());
92 92
$ssfPage = $pdf->open_pdi_page($ssfPDF, 1, ''); 93 93 $ssfPage = $pdf->open_pdi_page($ssfPDF, 1, '');
$ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width"); 94 94 $ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width");
$ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height"); 95 95 $ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height");
//print "Page width/height: $ssfWidth x $ssfHeight\n"; 96 96 //print "Page width/height: $ssfWidth x $ssfHeight\n";
97 97
$pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); 98 98 $pdf->begin_page_ext($ssfWidth, $ssfHeight, '');
99 99
$pdf->fit_pdi_page($ssfPage, 0, 0, ''); 100 100 $pdf->fit_pdi_page($ssfPage, 0, 0, '');
$pdf->close_pdi_page($ssfPage); 101 101 $pdf->close_pdi_page($ssfPage);
$pdf->close_pdi_document($ssfPDF); 102 102 $pdf->close_pdi_document($ssfPDF);
103 103
// write some text on to the page 104 104 // write some text on to the page
// for landscape mode... 105 105 // for landscape mode...
//$pdf->fit_textline("Back to Top", 20, 570, 106 106 //$pdf->fit_textline("Back to Top", 20, 570,
//$linkOpts = linkAttribs(1, 750); 107 107 //$linkOpts = linkAttribs(1, 750);
$action = $pdf->create_action("GoTo", "destination={page=1}"); 108 108 $action = $pdf->create_action("GoTo", "destination={page=1}");
$pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, 109 109 $pdf->fit_textline("Back to Top", 20, $ssfHeight - 30,
$textOpts . " underline=true matchbox={name=to_first_page}"); 110 110 $textOpts . " underline=true matchbox={name=to_first_page}");
111 111
// call create_annotation here 112 112 // call create_annotation here
$optlist = "action={activate " . $action . "} linewidth=0 " . 113 113 $optlist = "action={activate " . $action . "} linewidth=0 " .
"usematchbox={to_first_page}"; 114 114 "usematchbox={to_first_page}";
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); 115 115 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
116 116