Commit 48b48981311664d89558078b2e50a31ea98e2527

Authored by Brian Manning
1 parent 78894f99ac

pdflib_demo: added font search path config directive

Showing 1 changed file with 5 additions and 0 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/graphics/layers-and-bookmarks/php-layers-and-bookmarks/ 17 17 - https://www.pdflib.com/pdflib-cookbook/graphics/layers-and-bookmarks/php-layers-and-bookmarks/
- https://www.pdflib.com/pdflib-cookbook/text-output/create-interactive-index/php-create-interactive-index/ 18 18 - https://www.pdflib.com/pdflib-cookbook/text-output/create-interactive-index/php-create-interactive-index/
- 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/
- https://www.pdflib.com/en/pdflib-cookbook/block-handling-and-pps/linked-textblocks/php-linked-textblocks/ 20 20 - https://www.pdflib.com/en/pdflib-cookbook/block-handling-and-pps/linked-textblocks/php-linked-textblocks/
- https://www.pdflib.com/pdflib-cookbook/interactive-elements/link-annotations/php-link-annotations/ 21 21 - https://www.pdflib.com/pdflib-cookbook/interactive-elements/link-annotations/php-link-annotations/
- https://www.pdflib.com/pdflib-cookbook/text-output/footnotes-in-text/footnotes-in-text/ 22 22 - https://www.pdflib.com/pdflib-cookbook/text-output/footnotes-in-text/footnotes-in-text/
- https://www.pdflib.com/fileadmin/pdflib/Cookbook/pdf/footnotes_in_text.pdf 23 23 - https://www.pdflib.com/fileadmin/pdflib/Cookbook/pdf/footnotes_in_text.pdf
- https://www.pdflib.com/pdflib-cookbook/pdf-import/import-in-reverse-order/php-import-in-reverse-order/ 24 24 - https://www.pdflib.com/pdflib-cookbook/pdf-import/import-in-reverse-order/php-import-in-reverse-order/
25 25
*/ 26 26 */
27 27
// set a default timezone 28 28 // set a default timezone
date_default_timezone_set("America/Los_Angeles"); 29 29 date_default_timezone_set("America/Los_Angeles");
30 30
//require_once('tcpdf_include.php'); 31 31 //require_once('tcpdf_include.php');
$outfile = __DIR__ . '/pdflib_output.pdf'; 32 32 $outfile = __DIR__ . '/pdflib_output.pdf';
33 33
// page placement defaults 34 34 // page placement defaults
$pageTopYPortrait = 750; 35 35 $pageTopYPortrait = 750;
$pageTopYLandscape = 570; 36 36 $pageTopYLandscape = 570;
37 37
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 // all paths in 'searchpath' need to be inside curly braces
47 // multiple curly-braced paths in $searchpath need to be separated with
48 // space characters
49 $searchpath = '{/usr/share/fonts/dejavu/}';
50 $pdf->set_option("searchpath={" . $searchpath . "}");
46 51
if ($pdf->begin_document($outfile, "") == 0) 47 52 if ($pdf->begin_document($outfile, "") == 0)
throw new Exception("Error: " . $pdf->get_errmsg()); 48 53 throw new Exception("Error: " . $pdf->get_errmsg());
49 54
// set up the document metadata info 50 55 // set up the document metadata info
$pdf->set_info('Creator', 'IODP Science Support Office'); 51 56 $pdf->set_info('Creator', 'IODP Science Support Office');
$pdf->set_info('Author', 'IODP Science Support Office'); 52 57 $pdf->set_info('Author', 'IODP Science Support Office');
$pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals'); 53 58 $pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals');
$pdf->set_info('Subject', 'IODP Proposals'); 54 59 $pdf->set_info('Subject', 'IODP Proposals');
$pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition'); 55 60 $pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition');
56 61
$textOpts = "fontname={Helvetica} embedding fontsize=20 " 57 62 $textOpts = "fontname={Helvetica} embedding fontsize=20 "
. "encoding=unicode "; 58 63 . "encoding=unicode ";
59 64
// START PAGE 1 60 65 // START PAGE 1
$pdf->begin_page_ext(612, 792, ''); 61 66 $pdf->begin_page_ext(612, 792, '');
62 67
// create bookmarks first thing 63 68 // create bookmarks first thing
$action = $pdf->create_action("GoTo", "destination={page=1}"); 64 69 $action = $pdf->create_action("GoTo", "destination={page=1}");
$pdf->create_bookmark("Page 1", " action={activate= " . $action . "}"); 65 70 $pdf->create_bookmark("Page 1", " action={activate= " . $action . "}");
66 71
// action 67 72 // action
$action = $pdf->create_action("GoTo", "destination={page=2}"); 68 73 $action = $pdf->create_action("GoTo", "destination={page=2}");
// bookmark 69 74 // bookmark
$pdf->create_bookmark("Page 2", " action={activate= " . $action . "}"); 70 75 $pdf->create_bookmark("Page 2", " action={activate= " . $action . "}");
71 76
72 77
// text/image 73 78 // text/image
$pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, 74 79 $pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750,
$textOpts . " underline=true matchbox={name=to_second_page}"); 75 80 $textOpts . " underline=true matchbox={name=to_second_page}");
// annotation; reuse the "page 2" action above 76 81 // annotation; reuse the "page 2" action above
$optlist = "action={activate " . $action . "} linewidth=0 " . 77 82 $optlist = "action={activate " . $action . "} linewidth=0 " .
"usematchbox={to_second_page}"; 78 83 "usematchbox={to_second_page}";
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); 79 84 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
80 85
// the rest of the text on page 1 81 86 // the rest of the text on page 1
$pdf->fit_textline("This is some text on the first page; ", 20, 720, 82 87 $pdf->fit_textline("This is some text on the first page; ", 20, 720,
$textOpts); 83 88 $textOpts);
$pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts); 84 89 $pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts);
85 90
// END PAGE 1 86 91 // END PAGE 1
$pdf->end_page_ext(''); 87 92 $pdf->end_page_ext('');
88 93
// START PAGE 2 89 94 // START PAGE 2
//$pdf->begin_page_ext(612, 792, ''); 90 95 //$pdf->begin_page_ext(612, 792, '');
$ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", ''); 91 96 $ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", '');
if ($ssfPDF == 0) 92 97 if ($ssfPDF == 0)
throw new Exception("Error: " . $p->get_errmsg()); 93 98 throw new Exception("Error: " . $p->get_errmsg());
94 99
$ssfPage = $pdf->open_pdi_page($ssfPDF, 1, ''); 95 100 $ssfPage = $pdf->open_pdi_page($ssfPDF, 1, '');
$ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width"); 96 101 $ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width");
$ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height"); 97 102 $ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height");
//print "Page width/height: $ssfWidth x $ssfHeight\n"; 98 103 //print "Page width/height: $ssfWidth x $ssfHeight\n";
99 104
$pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); 100 105 $pdf->begin_page_ext($ssfWidth, $ssfHeight, '');
101 106
$pdf->fit_pdi_page($ssfPage, 0, 0, ''); 102 107 $pdf->fit_pdi_page($ssfPage, 0, 0, '');
$pdf->close_pdi_page($ssfPage); 103 108 $pdf->close_pdi_page($ssfPage);
$pdf->close_pdi_document($ssfPDF); 104 109 $pdf->close_pdi_document($ssfPDF);
105 110
// write some text on to the page 106 111 // write some text on to the page
// for landscape mode... 107 112 // for landscape mode...
//$pdf->fit_textline("Back to Top", 20, 570, 108 113 //$pdf->fit_textline("Back to Top", 20, 570,
//$linkOpts = linkAttribs(1, 750); 109 114 //$linkOpts = linkAttribs(1, 750);
$action = $pdf->create_action("GoTo", "destination={page=1}"); 110 115 $action = $pdf->create_action("GoTo", "destination={page=1}");
$pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, 111 116 $pdf->fit_textline("Back to Top", 20, $ssfHeight - 30,
$textOpts . " underline=true matchbox={name=to_first_page}"); 112 117 $textOpts . " underline=true matchbox={name=to_first_page}");
113 118
// call create_annotation here 114 119 // call create_annotation here
$optlist = "action={activate " . $action . "} linewidth=0 " . 115 120 $optlist = "action={activate " . $action . "} linewidth=0 " .
"usematchbox={to_first_page}"; 116 121 "usematchbox={to_first_page}";
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); 117 122 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
118 123
// END PAGE 2 119 124 // END PAGE 2
$pdf->end_page_ext(''); 120 125 $pdf->end_page_ext('');
121 126
// close the document 122 127 // close the document
$pdf->end_document(''); 123 128 $pdf->end_document('');
} 124 129 }
125 130
catch (PDFlibException $e) { 126 131 catch (PDFlibException $e) {
die("PDFlib exception occurred in sample:\n" . 127 132 die("PDFlib exception occurred in sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . 128 133 "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n"); 129 134 $e->get_errmsg() . "\n");
} 130 135 }
131 136
catch (Exception $e) { 132 137 catch (Exception $e) {
die($e); 133 138 die($e);
} 134 139 }
135 140
$p = 0; 136 141 $p = 0;
137 142
function linkAttribs($pageNum = 1, $pageY = 750) { 138 143 function linkAttribs($pageNum = 1, $pageY = 750) {
return "destination={page={$pageNum} type=fixed left=0 top={$pageY}}" 139 144 return "destination={page={$pageNum} type=fixed left=0 top={$pageY}}"
. " underline=true"; 140 145 . " underline=true";
} 141 146 }
142 147
/* 143 148 /*
// write out the contents of the first page 144 149 // write out the contents of the first page
$pdf->AddPage(); 145 150 $pdf->AddPage();
$topLink = $pdf->AddLink(); 146 151 $topLink = $pdf->AddLink();
$pdf->SetLink($topLink); 147 152 $pdf->SetLink($topLink);
//$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true); 148 153 //$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true); 149 154 $pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true);
//$pdf->SetFont('DejaVu', '', 20); 150 155 //$pdf->SetFont('DejaVu', '', 20);
$pdf->SetFont('dejavusanscondensed', '', 20); 151 156 $pdf->SetFont('dejavusanscondensed', '', 20);