Commit 3c0f907a9caec5dd9af931cb93265dc4a82b0992

Authored by Brian Manning
1 parent b3cc9cadc9

table_demo: added new relative font path to GNU FreeFonts

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