Commit 97614a87bf8e2a3afd38532be26ef7b83a36cb30

Authored by Brian Manning
1 parent ed360e2eb5

pdflib_demo: added bookmarks; reused the $action variable everywhere

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