You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
14 KiB

  1. <?php
  2. // Project: Web Reference Database (refbase) <http://www.refbase.net>
  3. // Copyright: Matthias Steffens <mailto:refbase@extracts.de> and the file's
  4. // original author(s).
  5. //
  6. // This code is distributed in the hope that it will be useful,
  7. // but WITHOUT ANY WARRANTY. Please see the GNU General Public
  8. // License for more details.
  9. //
  10. // File: ./cite/formats/cite_pdf.php
  11. // Repository: $HeadURL: file:///svn/p/refbase/code/branches/bleeding-edge/cite/formats/cite_pdf.php $
  12. // Author(s): Matthias Steffens <mailto:refbase@extracts.de>
  13. //
  14. // Created: 10-Jun-06, 02:04
  15. // Modified: $Date: 2017-04-13 02:00:18 +0000 (Thu, 13 Apr 2017) $
  16. // $Author: karnesky $
  17. // $Revision: 1416 $
  18. // This is a citation format file (which must reside within the 'cite/formats/' sub-directory of your refbase root directory). It contains a
  19. // version of the 'citeRecords()' function that outputs a reference list from selected records in PDF format.
  20. // PDF format specification is available at <http://partners.adobe.com/public/developer/pdf/index_reference.html>, more info at <http://en.wikipedia.org/wiki/PDF>
  21. // LIMITATIONS: - export of cited references to PDF does currently only work with a latin1 database but not with UTF-8 (since I don't know how to write Unicode characters to PDF)
  22. // - there's currently no conversion of en-/emdashes or markup for greek letters and super-/subscript (reasons are that I don't know how to print chars by code number)
  23. // --------------------------------------------------------------------
  24. // Include the pdf-php package
  25. require_once 'includes/classes/org/pdf-php/class.ezpdf.php';
  26. // --- BEGIN CITATION FORMAT ---
  27. // Requires the "PHP Pdf creation (pdf-php)" Package (by Wayne Munro, R&OS Ltd), which is available
  28. // under a public domain licence: <http://www.ros.co.nz/pdf>
  29. function citeRecords($result, $rowsFound, $query, $queryURL, $showQuery, $showLinks, $rowOffset, $showRows, $previousOffset, $nextOffset, $wrapResults, $citeStyle, $citeOrder, $citeType, $orderBy, $headerMsg, $userID, $viewType)
  30. {
  31. global $officialDatabaseName; // these variables are defined in 'ini.inc.php'
  32. global $databaseBaseURL;
  33. global $contentTypeCharset;
  34. global $pdfPageSize;
  35. global $client;
  36. // The array '$transtab_refbase_pdf' contains search & replace patterns for conversion from refbase markup to PDf markup & entities
  37. global $transtab_refbase_pdf; // defined in 'transtab_refbase_pdf.inc.php'
  38. // Initialize array variables:
  39. $yearsArray = array();
  40. $typeTitlesArray = array();
  41. // Define inline text markup to be used by the 'citeRecord()' function:
  42. $markupPatternsArray = array("bold-prefix" => "<b>", // html-style fontshape markup is recognized and converted by the pdf-php package
  43. "bold-suffix" => "</b>",
  44. "italic-prefix" => "<i>",
  45. "italic-suffix" => "</i>",
  46. "underline-prefix" => "<u>",
  47. "underline-suffix" => "</u>",
  48. "endash" => "", // see notes for "*-quote-*" below; we could also use "�" here
  49. "emdash" => "", // an emdash might also be faked with two endashes ("��")
  50. "ampersand" => "&",
  51. "double-quote" => '"',
  52. "double-quote-left" => "", // AFAIK, the ISO-8859-1 (latin1) character set does not have any curly quotes,
  53. "double-quote-right" => "", // see e.g. <http://www.ramsch.org/martin/uni/fmi-hp/iso8859-1.html>; but ...
  54. "single-quote" => "'",
  55. "single-quote-left" => "", // ... since the pdf-php package let's you replace an (unused) character for any other PostScript char (see below), we use
  56. "single-quote-right" => "", // the '$diff' array below to replace e.g. "�" with a single left curly quote and "�" with a single right curly quote, etc
  57. "less-than" => "<",
  58. "greater-than" => ">",
  59. "newline" => "\n"
  60. );
  61. // Defines search & replace 'actions' that will be applied upon PDF output to all those refbase fields that are listed
  62. // in the corresponding 'fields' element:
  63. $pdfSearchReplaceActionsArray = array(
  64. array('fields' => array("title", "publication", "abbrev_journal", "address", "keywords", "abstract", "orig_title", "series_title", "abbrev_series_title", "notes"),
  65. 'actions' => $transtab_refbase_pdf
  66. )
  67. );
  68. // For CLI queries, we'll allow paging thru the result set, i.e. we honour the values of the CLI options '-S|--start' ('$rowOffset')
  69. // and '-R|--rows' ('$showRows') ('$rowOffset' and '$showRows' are re-assigned in function 'seekInMySQLResultsToOffset()' in 'include.inc.php')
  70. if (preg_match("/^cli/i", $client)) // if the query originated from a command line client such as the "refbase" CLI client ("cli-refbase-1.0")
  71. $showMaxRows = $showRows; // show only rows up to the value given in '$showRows'
  72. else
  73. $showMaxRows = $rowsFound; // otherwise show all rows
  74. // Setup the basic PDF document structure (PDF functions defined in 'class.ezpdf.php'):
  75. $pdf = new Cezpdf($pdfPageSize, 'portrait'); // initialize PDF object
  76. if (!empty($headerMsg)) // adjust upper page margin if a custom header message was given
  77. $pageMarginTop = "70";
  78. else
  79. $pageMarginTop = "50";
  80. $pdf -> ezSetMargins($pageMarginTop, 70, 50, 50); // set document margins (top, bottom, left, right)
  81. // Set fonts:
  82. $headingFont = 'includes/classes/org/pdf-php/fonts/Helvetica.afm';
  83. $textBodyFont = 'includes/classes/org/pdf-php/fonts/Times-Roman.afm';
  84. // Re-map character numbers from the 0->255 range to a named character, i.e. replace an (unused) character for any other PostScript char;
  85. // see the PDF reference for a list of supported PostScript/PDF character names: <http://www.adobe.com/devnet/pdf/pdf_reference.html>;
  86. // for the decimal code numbers of the ISO-8859-1 character set, see e.g.: <http://www.ramsch.org/martin/uni/fmi-hp/iso8859-1.html>
  87. $diff = array(
  88. 166 => 'endash', // "�"
  89. 169 => 'emdash', // "�"
  90. 170 => 'quotedblleft', // "�"
  91. 172 => 'quotedblright', // "�"
  92. 174 => 'quoteleft', // "�"
  93. 182 => 'quoteright' // "�"
  94. );
  95. // Select a font:
  96. $pdf->selectFont($textBodyFont, array('encoding' => 'WinAnsiEncoding', 'differences' => $diff));
  97. $pdf->openHere('Fit');
  98. // Put a footer (and optionally a header) on all the pages:
  99. $all = $pdf->openObject(); // start an independent object; all further writes to a page will actually go into this object, until a 'closeObject()' call is made
  100. $pdf->saveState();
  101. $pdf->setStrokeColor(0, 0, 0, 1); // set line color
  102. $pdf->setLineStyle(0.5); // set line width
  103. // - print header line and header message at the specified x/y position:
  104. if (!empty($headerMsg)) // if a custom header message was given
  105. {
  106. // Remove any colon (":") from end of header message:
  107. $headerMsg = trimTextPattern($headerMsg, ":", false, true); // function 'trimTextPattern()' is defined in 'include.inc.php'
  108. // Decode any HTML entities:
  109. // (these may occur in the header message e.g. if the user's preferred display language is not English but German or French, etc)
  110. $headerMsg = decodeHTML($contentTypeCharset, $headerMsg); // function 'decodeHTML()' is defined in 'include.inc.php', and '$contentTypeCharset' is defined in 'ini.inc.php'
  111. // Convert refbase markup in the header message into appropriate PDF markup & entities:
  112. $headerMsg = searchReplaceText($transtab_refbase_pdf, $headerMsg, true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  113. if ($pdfPageSize == 'a4') // page dimensions 'a4': 595.28 x 841.89 pt.
  114. {
  115. $pdf->line(20, 800, 575, 800);
  116. $pdf->addText(50, 805, 10, $headerMsg);
  117. }
  118. elseif ($pdfPageSize == 'letter') // page dimensions 'letter': 612 x 792 pt.
  119. {
  120. $pdf->line(20, 750, 592, 750);
  121. $pdf->addText(50, 755, 10, $headerMsg);
  122. }
  123. }
  124. // - print footer line and footer text at the specified x/y position:
  125. if ($pdfPageSize == 'a4')
  126. {
  127. $pdf->line(20, 40, 575, 40);
  128. $pdf->addText(50, 28, 10, $officialDatabaseName . ' � ' . $databaseBaseURL); // w.r.t. the endash, see notes at '$markupPatternsArray' and '$diff' above
  129. }
  130. elseif ($pdfPageSize == 'letter')
  131. {
  132. $pdf->line(20, 40, 592, 40);
  133. $pdf->addText(50, 28, 10, $officialDatabaseName . ' � ' . $databaseBaseURL);
  134. }
  135. $pdf->restoreState();
  136. $pdf->closeObject(); // close the currently open object; further writes will now go to the current page
  137. $pdf->addObject($all, 'all'); // note that object can be told to appear on just odd or even pages by changing 'all' to 'odd' or 'even'
  138. // Start printing page numbers:
  139. if ($pdfPageSize == 'a4')
  140. {
  141. $pdf->ezStartPageNumbers(550, 28, 10, '', '', 1);
  142. }
  143. elseif ($pdfPageSize == 'letter')
  144. {
  145. $pdf->ezStartPageNumbers(567, 28, 10, '', '', 1);
  146. }
  147. // LOOP OVER EACH RECORD:
  148. // Fetch one page of results (or less if on the last page)
  149. // (i.e., upto the limit specified in $showMaxRows) fetch a row into the $row array and ...
  150. for ($rowCounter=0; (($rowCounter < $showMaxRows) && ($row = @ mysqli_fetch_array($result))); $rowCounter++)
  151. {
  152. foreach ($row as $rowFieldName => $rowFieldValue)
  153. // Apply search & replace 'actions' to all fields that are listed in the 'fields' element of the arrays contained in '$pdfSearchReplaceActionsArray':
  154. foreach ($pdfSearchReplaceActionsArray as $fieldActionsArray)
  155. if (in_array($rowFieldName, $fieldActionsArray['fields']))
  156. $row[$rowFieldName] = searchReplaceText($fieldActionsArray['actions'], $row[$rowFieldName], true); // function 'searchReplaceText()' is defined in 'include.inc.php'
  157. // Order attributes according to the chosen output style & record type:
  158. $record = citeRecord($row, $citeStyle, $citeType, $markupPatternsArray, false); // function 'citeRecord()' is defined in the citation style file given in '$citeStyleFile' (which, in turn, must reside in the 'cite' directory of the refbase root directory), see function 'generateCitations()'
  159. // Print out the current record:
  160. if (!empty($record)) // unless the record buffer is empty...
  161. {
  162. // Print any section heading(s):
  163. if (preg_match("/year|type/i", $citeOrder))
  164. {
  165. $headingPrefix = "";
  166. $headingSuffix = "";
  167. $sectionMarkupPrefix = "<b>";
  168. $sectionMarkupSuffix = "</b>\n";
  169. $subSectionMarkupPrefix = "";
  170. $subSectionMarkupSuffix = "\n";
  171. if ($citeOrder == "type-year")
  172. $sectionMarkupSuffix .= "\n";
  173. list($yearsArray, $typeTitlesArray, $sectionHeading) = generateSectionHeading($yearsArray, $typeTitlesArray, $row, $citeOrder, $headingPrefix, $headingSuffix, $sectionMarkupPrefix, $sectionMarkupSuffix, $subSectionMarkupPrefix, $subSectionMarkupSuffix); // function 'generateSectionHeading()' is defined in 'cite.inc.php'
  174. if (!empty($sectionHeading))
  175. {
  176. $pdf->selectFont($headingFont, array('encoding' => 'WinAnsiEncoding', 'differences' => $diff)); // use Helvetica
  177. $pdf->ezText($sectionHeading, '14', array('justification' => 'left')); // create heading using a font size of 14pt
  178. }
  179. }
  180. // If character encoding is not UTF-8 already, convert record text to UTF-8:
  181. // if ($contentTypeCharset != "UTF-8")
  182. // $record = convertToCharacterEncoding("UTF-8", "IGNORE", $record); // function 'convertToCharacterEncoding()' is defined in 'include.inc.php'
  183. // NOTE: Export of cited references to PDF does currently only work with a latin1 database but not with UTF-8 (since I don't know how to write Unicode characters to PDF).
  184. // As a workaround, we could convert UTF-8 characters to latin1 if possible (and omit any other higher ASCII chars)
  185. // TODO: While this workaround indeed fixes display issues with higher ASCII chars that have equivalents in the latin1 charset, this will currently swallow higher ASCII
  186. // hyphens/dashes such as endashes (which display correctly without this workaround).
  187. // if ($contentTypeCharset == "UTF-8")
  188. // $record = convertToCharacterEncoding("ISO-8859-1", "TRANSLIT", $record, "UTF-8"); // function 'convertToCharacterEncoding()' is defined in 'include.inc.php'
  189. // Set paragraph text options:
  190. $textOptions = array(
  191. 'justification' => 'full'
  192. // 'aleft' => '50',
  193. // 'aright' => '545'
  194. );
  195. // possible array options:
  196. // 'left'=> number, gap to leave from the left margin
  197. // 'right'=> number, gap to leave from the right margin
  198. // 'aleft'=> number, absolute left position (overrides 'left')
  199. // 'aright'=> number, absolute right position (overrides 'right')
  200. // 'justification' => 'left','right','center','centre','full'
  201. //
  202. // only set one of the next two items (leading overrides spacing)
  203. // 'leading' => number, defines the total height taken by the line, independent of the font height.
  204. // 'spacing' => a real number, though usually set to one of 1, 1.5, 2 (line spacing as used in word processing)
  205. // Write PDF paragraph:
  206. $pdf->selectFont($textBodyFont, array('encoding' => 'WinAnsiEncoding')); // use Times-Roman
  207. $pdf->ezText($record . "\n", '12', $textOptions); // create text block with record text using "Times Roman" and a font size of 12pt
  208. }
  209. }
  210. return $pdf->ezStream();
  211. }
  212. // --- END CITATION FORMAT ---
  213. ?>