Compare View
Commits (2)
Diff
Showing 1 changed file Inline Diff
pdflib_demo
View file @
97614a8
#!/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"); | |
96 | //print "Page width/height: $ssfWidth x $ssfHeight\n"; | |||
//print "Page width/height: $ssfWidth x $ssfHeight\n"; | 79 | 97 | ||
80 | 98 | $pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); | ||
$pdf->begin_page_ext($ssfWidth, $ssfHeight, ''); | 81 | 99 | ||
82 | 100 | $pdf->fit_pdi_page($ssfPage, 0, 0, ''); | ||
$pdf->fit_pdi_page($ssfPage, 0, 0, ''); | 83 | 101 | $pdf->close_pdi_page($ssfPage); | |
$pdf->close_pdi_page($ssfPage); | 84 | 102 | $pdf->close_pdi_document($ssfPDF); | |
$pdf->close_pdi_document($ssfPDF); | 85 | 103 | ||
86 | 104 | // write some text on to the page | ||
// write some text on to the page | 87 | 105 | // for landscape mode... | |
// for landscape mode... | 88 | 106 | //$pdf->fit_textline("Back to Top", 20, 570, | |
//$pdf->fit_textline("Back to Top", 20, 570, | 89 | 107 | //$linkOpts = linkAttribs(1, 750); | |
//$linkOpts = linkAttribs(1, 750); | 90 | 108 | $action = $pdf->create_action("GoTo", "destination={page=1}"); | |
#$actionList = "destination={page=1 type=fixed left=0 top=750}"; | 91 | 109 | $pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, | |
$actionList = "destination={page=1}"; | 92 | 110 | $textOpts . " underline=true matchbox={name=to_first_page}"); | |
$action = $pdf->create_action("GoTo", $actionList); | 93 | 111 | ||
112 | // call create_annotation here | |||
113 | $optlist = "action={activate " . $action . "} linewidth=0 " . | |||
114 | "usematchbox={to_first_page}"; | |||
115 | $pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); | |||
$pdf->fit_textline("Back to Top", 20, $ssfHeight - 30, | 94 | 116 | ||
$textOpts . " underline=true matchbox={name=first_page}"); | 95 | 117 | // END PAGE 2 | |
96 | 118 | $pdf->end_page_ext(''); | ||
// FIXME call create_annotation here | 97 | 119 | ||
$optlist = "action={activate " . $action . "} linewidth=1 " . | 98 | 120 | // close the document | |
"usematchbox={first_page}"; | 99 | 121 | $pdf->end_document(''); | |
$pdf->create_annotation(0, 0, 0, 0, "Link", $optlist); | 100 | |||
101 | 122 | } | ||
// END PAGE 2 | 102 | 123 | ||
$pdf->end_page_ext(''); | 103 | 124 | catch (PDFlibException $e) { | |
104 | 125 | die("PDFlib exception occurred in sample:\n" . | ||
// close the document | 105 | 126 | "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . | |
$pdf->end_document(''); | 106 | 127 | $e->get_errmsg() . "\n"); | |
107 | 128 | } | ||
} | 108 | 129 | ||
109 | 130 | catch (Exception $e) { | ||
catch (PDFlibException $e) { | 110 | 131 | die($e); | |
die("PDFlib exception occurred in sample:\n" . | 111 | 132 | } | |
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " . | 112 | 133 | ||
$e->get_errmsg() . "\n"); | 113 | 134 | $p = 0; | |
} | 114 | 135 | ||
115 | 136 | function linkAttribs($pageNum = 1, $pageY = 750) { | ||
catch (Exception $e) { | 116 | 137 | return "destination={page={$pageNum} type=fixed left=0 top={$pageY}}" | |
die($e); | 117 | 138 | . " underline=true"; | |
} | 118 | 139 | } | |
119 | 140 | |||
$p = 0; | 120 | 141 | /* | |
121 | 142 | // write out the contents of the first page | ||
function linkAttribs($pageNum = 1, $pageY = 750) { | 122 | 143 | $pdf->AddPage(); | |
return "destination={page={$pageNum} type=fixed left=0 top={$pageY}}" | 123 | 144 | $topLink = $pdf->AddLink(); | |
. " underline=true"; | 124 | 145 | $pdf->SetLink($topLink); | |
} | 125 | 146 | //$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true); | |
126 | 147 | $pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true); | ||
/* | 127 | 148 | //$pdf->SetFont('DejaVu', '', 20); | |
// write out the contents of the first page | 128 | 149 | $pdf->SetFont('dejavusanscondensed', '', 20); | |
$pdf->AddPage(); | 129 | 150 | $pdf->SetFont('', 'U'); | |
$topLink = $pdf->AddLink(); | 130 | 151 | $ssfLink = $pdf->AddLink(); | |
$pdf->SetLink($topLink); | 131 | 152 | $pdf->Write(10, "Jump to SSF for SHACK-10A", $ssfLink); | |
//$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true); | 132 | 153 | $pdf->SetFont(''); | |
$pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true); | 133 | 154 | $pdf->Ln(); | |
//$pdf->SetFont('DejaVu', '', 20); | 134 | 155 | $pdf->Write(10, "This is some text on the first page; "); | |
$pdf->SetFont('dejavusanscondensed', '', 20); | 135 | 156 | $pdf->Ln(); | |
$pdf->SetFont('', 'U'); | 136 | 157 | $pdf->Write(10, "Привет! Этот немного текста."); | |
$ssfLink = $pdf->AddLink(); | 137 | 158 | ||
$pdf->Write(10, "Jump to SSF for SHACK-10A", $ssfLink); | 138 | 159 | // load up the Site Summary Figure to go on page #2 | |
$pdf->SetFont(''); | 139 | 160 | $ssfPageCount = $pdf->setSourceFile(__DIR__ . '/site_summary_figure.pdf'); | |
$pdf->Ln(); | 140 | 161 | //print "Page count of site summary figure form is $ssfPageCount\n"; | |
$pdf->Write(10, "This is some text on the first page; "); | 141 | 162 | ||
$pdf->Ln(); | 142 | 163 | // import the SSF page | |
$pdf->Write(10, "Привет! Этот немного текста."); | 143 | 164 | $ssfTemplate= $pdf->importPage(1); | |
144 | 165 | //print "Page size of site summary figure PDF is: "; | ||
// load up the Site Summary Figure to go on page #2 | 145 | 166 | //$ssfSize = $pdf->getTemplateSize($ssfTemplate); |