Commit 5b784109da223e537ed2f71f9280f5ec09386639

Authored by Brian Manning
1 parent 12169463de

pdflib_demo: basic text drawing and writing of the output PDF works

Showing 1 changed file with 51 additions and 10 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 17
PDFlib was loaded via a Homebrew package for working with this demo 18
*/ 19 18 */
20 19
$loader = require __DIR__ . '/vendor/autoload.php'; 21
$loader->register(); 22
23
// set a default timezone 24 20 // set a default timezone
date_default_timezone_set("America/Los_Angeles"); 25 21 date_default_timezone_set("America/Los_Angeles");
26 22
//require_once('tcpdf_include.php'); 27 23 //require_once('tcpdf_include.php');
//$pdf = new TCPDF('p', 'mm', 'Letter', TRUE, 'UTF-8'); 28 24 $outfile = __DIR__ . '/output.pdf';
class_exists('TCPDF', true); 29
$pdf = new FPDI('p', 'mm', 'Letter', TRUE, 'UTF-8'); 30
// disable headers and footers on all pages 31
$pdf->setPrintHeader(FALSE); 32
$pdf->setPrintFooter(FALSE); 33
34 25
26 // wrap PDFlib usage in a try{} block
27 try {
28 $pdf = new PDFlib();
29
30 // set up some PDFlib options
31 $pdf->set_option("errorpolicy=return");
32 $pdf->set_option("stringformat=utf8");
33
34 if ($pdf->begin_document($outfile, "") == 0)
35 throw new Exception("Error: " . $pdf->get_errmsg());
36
37 // set up the document metadata info
38 $pdf->set_info('Creator', 'IODP Science Support Office');
39 $pdf->set_info('Author', 'IODP Science Support Office');
40 $pdf->set_info('Title', 'Demo of TCPDF for generating IODP Proposals');
41 $pdf->set_info('Subject', 'IODP Proposals');
42 $pdf->set_info('Keywords', 'IODP proposal ocean drilling core expedition');
43
44 $pdf->begin_page_ext(612, 792, '');
45
46 $textOpts = "fontname={Helvetica} embedding fontsize=20 "
47 . "encoding=unicode ";
48
49 $pdf->fit_textline("Jump to SSF for SHACK-10A", 20, 750, $textOpts);
50 $pdf->fit_textline("This is some text on the first page; ", 20, 720,
51 $textOpts);
52 $pdf->fit_textline("Привет! Этот немного текста.", 20, 690, $textOpts);
53
54 // finish this page
55 $pdf->end_page_ext('');
56
57 // close the document
58 $pdf->end_document('');
59
60 }
61
62 catch (PDFlibException $e) {
63 die("PDFlib exception occurred in sample:\n" .
64 "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
65 $e->get_errmsg() . "\n");
66 }
67
68 catch (Exception $e) {
69 die($e);
70 }
71
72 $p = 0;
73
74 /*
// write out the contents of the first page 35 75 // write out the contents of the first page
$pdf->AddPage(); 36 76 $pdf->AddPage();
$topLink = $pdf->AddLink(); 37 77 $topLink = $pdf->AddLink();
$pdf->SetLink($topLink); 38 78 $pdf->SetLink($topLink);
//$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true); 39 79 //$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true); 40 80 $pdf->AddFont('dejavusanscondensed','','dejavusanscondensed.php',true);
//$pdf->SetFont('DejaVu', '', 20); 41 81 //$pdf->SetFont('DejaVu', '', 20);
$pdf->SetFont('dejavusanscondensed', '', 20); 42 82 $pdf->SetFont('dejavusanscondensed', '', 20);
$pdf->SetFont('', 'U'); 43 83 $pdf->SetFont('', 'U');
$ssfLink = $pdf->AddLink(); 44 84 $ssfLink = $pdf->AddLink();
$pdf->Write(10, "Jump to SSF for SHACK-10A", $ssfLink); 45 85 $pdf->Write(10, "Jump to SSF for SHACK-10A", $ssfLink);
$pdf->SetFont(''); 46 86 $pdf->SetFont('');
$pdf->Ln(); 47 87 $pdf->Ln();
$pdf->Write(10, "This is some text on the first page; "); 48 88 $pdf->Write(10, "This is some text on the first page; ");
$pdf->Ln(); 49 89 $pdf->Ln();
$pdf->Write(10, "Привет! Этот немного текста."); 50 90 $pdf->Write(10, "Привет! Этот немного текста.");
51 91
// load up the Site Summary Figure to go on page #2 52 92 // load up the Site Summary Figure to go on page #2
$ssfPageCount = $pdf->setSourceFile(__DIR__ . '/site_summary_figure.pdf'); 53 93 $ssfPageCount = $pdf->setSourceFile(__DIR__ . '/site_summary_figure.pdf');
//print "Page count of site summary figure form is $ssfPageCount\n"; 54 94 //print "Page count of site summary figure form is $ssfPageCount\n";
55 95
// import the SSF page 56 96 // import the SSF page
$ssfTemplate= $pdf->importPage(1); 57 97 $ssfTemplate= $pdf->importPage(1);
//print "Page size of site summary figure PDF is: "; 58 98 //print "Page size of site summary figure PDF is: ";
//$ssfSize = $pdf->getTemplateSize($ssfTemplate); 59 99 //$ssfSize = $pdf->getTemplateSize($ssfTemplate);
//print "{$ssfSize['w']}mm x {$ssfSize['h']}mm\n"; 60 100 //print "{$ssfSize['w']}mm x {$ssfSize['h']}mm\n";
61 101
// add the SSF file to the 2nd page 62 102 // add the SSF file to the 2nd page
$pdf->AddPage(); 63 103 $pdf->AddPage();
$pdf->SetLink($ssfLink); 64 104 $pdf->SetLink($ssfLink);
$pdf->useTemplate($ssfTemplate); 65 105 $pdf->useTemplate($ssfTemplate);
$pdf->SetFont('Helvetica'); 66 106 $pdf->SetFont('Helvetica');
$pdf->SetXY(5,5); 67 107 $pdf->SetXY(5,5);
$pdf->SetFont('', 'U'); 68 108 $pdf->SetFont('', 'U');