Commit 3052e0fcbdc480b2ec1220dd658d3a69a5ce68c3

Authored by Brian Manning
1 parent 97614a87bf

pdflib_demo: Added more bookmarks

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