modules/total_pages.js

  1. /**
  2. * @license
  3. * ====================================================================
  4. * Copyright (c) 2013 Eduardo Menezes de Morais, eduardo.morais@usp.br
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. * ====================================================================
  25. */
  26. import { jsPDF } from "../jspdf.js";
  27. /**
  28. * jsPDF total_pages plugin
  29. * @name total_pages
  30. * @module
  31. */
  32. (function(jsPDFAPI) {
  33. "use strict";
  34. /**
  35. * @name putTotalPages
  36. * @function
  37. * @param {string} pageExpression Regular Expression
  38. * @returns {jsPDF} jsPDF-instance
  39. */
  40. jsPDFAPI.putTotalPages = function(pageExpression) {
  41. "use strict";
  42. var replaceExpression;
  43. var totalNumberOfPages = 0;
  44. if (parseInt(this.internal.getFont().id.substr(1), 10) < 15) {
  45. replaceExpression = new RegExp(pageExpression, "g");
  46. totalNumberOfPages = this.internal.getNumberOfPages();
  47. } else {
  48. replaceExpression = new RegExp(
  49. this.pdfEscape16(pageExpression, this.internal.getFont()),
  50. "g"
  51. );
  52. totalNumberOfPages = this.pdfEscape16(
  53. this.internal.getNumberOfPages() + "",
  54. this.internal.getFont()
  55. );
  56. }
  57. for (var n = 1; n <= this.internal.getNumberOfPages(); n++) {
  58. for (var i = 0; i < this.internal.pages[n].length; i++) {
  59. this.internal.pages[n][i] = this.internal.pages[n][i].replace(
  60. replaceExpression,
  61. totalNumberOfPages
  62. );
  63. }
  64. }
  65. return this;
  66. };
  67. })(jsPDF.API);