Commit ed360e2eb57b39905f33508fe03714febfeb397b

Authored by Brian Manning
1 parent 9d13742da4

pdflib_demo: linking from the second page to the first page works now

Showing 1 changed file with 13 additions and 6 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
// START PAGE 1 55 55 // START PAGE 1
$pdf->begin_page_ext(612, 792, ''); 56 56 $pdf->begin_page_ext(612, 792, '');
57 57
$textOpts = "fontname={Helvetica} embedding fontsize=20 " 58 58 $textOpts = "fontname={Helvetica} embedding fontsize=20 "
. "encoding=unicode "; 59 59 . "encoding=unicode ";
60 60
$pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, 61 61 $pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750,
$textOpts . " underline=true"); 62 62 $textOpts); # . " underline=true matchbox={name=first_page}");
$pdf->fit_textline("This is some text on the first page; ", 20, 720, 63 63 $pdf->fit_textline("This is some text on the first page; ", 20, 720,
$textOpts); 64 64 $textOpts); # . " matchbox={name=first_page}");
$pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts); 65 65 $pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts);
66 66
// END PAGE 1 67 67 // END PAGE 1
$pdf->end_page_ext(''); 68 68 $pdf->end_page_ext('');
69 69
// START PAGE 2 70 70 // START PAGE 2
//$pdf->begin_page_ext(612, 792, ''); 71 71 //$pdf->begin_page_ext(612, 792, '');
$ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", ''); 72 72 $ssfPDF = $pdf->open_pdi_document(__DIR__ . "/site_summary_figure.pdf", '');
if ($ssfPDF == 0) 73 73 if ($ssfPDF == 0)
throw new Exception("Error: " . $p->get_errmsg()); 74 74 throw new Exception("Error: " . $p->get_errmsg());
75 75
$ssfPage = $pdf->open_pdi_page($ssfPDF, 1, ''); 76 76 $ssfPage = $pdf->open_pdi_page($ssfPDF, 1, '');
$ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width"); 77 77 $ssfWidth = $pdf->pcos_get_number($ssfPDF, "pages[0]/width");
$ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height"); 78 78 $ssfHeight = $pdf->pcos_get_number($ssfPDF, "pages[0]/height");
79 //print "Page width/height: $ssfWidth x $ssfHeight\n";
79 80
$pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); 80 81 $pdf->begin_page_ext($ssfWidth, $ssfHeight, '');
81 82
$pdf->fit_pdi_page($ssfPage, 0, 0, ''); 82 83 $pdf->fit_pdi_page($ssfPage, 0, 0, '');
$pdf->close_pdi_page($ssfPage); 83 84 $pdf->close_pdi_page($ssfPage);
$pdf->close_pdi_document($ssfPDF); 84 85 $pdf->close_pdi_document($ssfPDF);
85 86
// write some text on to the page 86 87 // write some text on to the page
// for landscape mode... 87 88 // for landscape mode...
//$pdf->fit_textline("Back to Top", 20, 570, 88 89 //$pdf->fit_textline("Back to Top", 20, 570,
//$linkOpts = linkAttribs(1, 750); 89 90 //$linkOpts = linkAttribs(1, 750);
$optList = "destination={page=1 type=fixed left=0 top=750}"; 90 91 #$actionList = "destination={page=1 type=fixed left=0 top=750}";
$action = $p->create_action("GoTo", $optList); 91 92 $actionList = "destination={page=1}";
print_r($linkOpts); 92 93 $action = $pdf->create_action("GoTo", $actionList);
$pdf->fit_textline("Back to Top", 20, 750, $linkOpts); 93 94 $pdf->fit_textline("Back to Top", 20, $ssfHeight - 30,
95 $textOpts . " underline=true matchbox={name=first_page}");
96
97 // FIXME call create_annotation here
98 $optlist = "action={activate " . $action . "} linewidth=1 " .
99 "usematchbox={first_page}";
100 $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist);
94 101
// END PAGE 2 95 102 // END PAGE 2
$pdf->end_page_ext(''); 96 103 $pdf->end_page_ext('');
97 104
// close the document 98 105 // close the document
$pdf->end_document(''); 99 106 $pdf->end_document('');
100 107
} 101 108 }
102 109
catch (PDFlibException $e) { 103 110 catch (PDFlibException $e) {
die("PDFlib exception occurred in sample:\n" . 104 111 die("PDFlib exception occurred in sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . 105 112 "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n"); 106 113 $e->get_errmsg() . "\n");
} 107 114 }
108 115
catch (Exception $e) { 109 116 catch (Exception $e) {
die($e); 110 117 die($e);
} 111 118 }
112 119
$p = 0; 113 120 $p = 0;
114 121
function linkAttribs($pageNum = 1, $pageY = 750) { 115 122 function linkAttribs($pageNum = 1, $pageY = 750) {
return "destination={page={$pageNum} type=fixed left=0 top={$pageY}}" 116 123 return "destination={page={$pageNum} type=fixed left=0 top={$pageY}}"
. " underline=true"; 117 124 . " underline=true";
} 118 125 }
119 126
/* 120 127 /*
// write out the contents of the first page 121 128 // write out the contents of the first page
$pdf->AddPage(); 122 129 $pdf->AddPage();
$topLink = $pdf->AddLink(); 123 130 $topLink = $pdf->AddLink();
$pdf->SetLink($topLink); 124 131 $pdf->SetLink($topLink);
//$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true); 125 132 //$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true); 126 133 $pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true);
//$pdf->SetFont('DejaVu', '', 20); 127 134 //$pdf->SetFont('DejaVu', '', 20);
$pdf->SetFont('dejavusanscondensed', '', 20); 128 135 $pdf->SetFont('dejavusanscondensed', '', 20);
$pdf->SetFont('', 'U'); 129 136 $pdf->SetFont('', 'U');
$ssfLink = $pdf->AddLink(); 130 137 $ssfLink = $pdf->AddLink();
$pdf->Write(10, "Jump to SSF for SHACK-10A", $ssfLink); 131 138 $pdf->Write(10, "Jump to SSF for SHACK-10A", $ssfLink);
$pdf->SetFont(''); 132 139 $pdf->SetFont('');
$pdf->Ln(); 133 140 $pdf->Ln();
$pdf->Write(10, "This is some text on the first page; "); 134 141 $pdf->Write(10, "This is some text on the first page; ");
$pdf->Ln(); 135 142 $pdf->Ln();
$pdf->Write(10, "Привет! Этот немного текста."); 136 143 $pdf->Write(10, "Привет! Этот немного текста.");
137 144
// load up the Site Summary Figure to go on page #2 138 145 // load up the Site Summary Figure to go on page #2