modules/vfs.js

  1. /**
  2. * @license
  3. * jsPDF virtual FileSystem functionality
  4. *
  5. * Licensed under the MIT License.
  6. * http://opensource.org/licenses/mit-license
  7. */
  8. import { jsPDF } from "../jspdf.js";
  9. /**
  10. * Use the vFS to handle files
  11. *
  12. * @name vFS
  13. * @module
  14. */
  15. (function(jsPDFAPI) {
  16. "use strict";
  17. var _initializeVFS = function() {
  18. if (typeof this.internal.vFS === "undefined") {
  19. this.internal.vFS = {};
  20. }
  21. return true;
  22. };
  23. /**
  24. * Check if the file exists in the vFS
  25. *
  26. * @name existsFileInVFS
  27. * @function
  28. * @param {string} Possible filename in the vFS.
  29. * @returns {boolean}
  30. * @example
  31. * doc.existsFileInVFS("someFile.txt");
  32. */
  33. jsPDFAPI.existsFileInVFS = function(filename) {
  34. _initializeVFS.call(this);
  35. return typeof this.internal.vFS[filename] !== "undefined";
  36. };
  37. /**
  38. * Add a file to the vFS
  39. *
  40. * @name addFileToVFS
  41. * @function
  42. * @param {string} filename The name of the file which should be added.
  43. * @param {string} filecontent The content of the file.
  44. * @returns {jsPDF}
  45. * @example
  46. * doc.addFileToVFS("someFile.txt", "BADFACE1");
  47. */
  48. jsPDFAPI.addFileToVFS = function(filename, filecontent) {
  49. _initializeVFS.call(this);
  50. this.internal.vFS[filename] = filecontent;
  51. return this;
  52. };
  53. /**
  54. * Get the file from the vFS
  55. *
  56. * @name getFileFromVFS
  57. * @function
  58. * @param {string} The name of the file which gets requested.
  59. * @returns {string}
  60. * @example
  61. * doc.getFileFromVFS("someFile.txt");
  62. */
  63. jsPDFAPI.getFileFromVFS = function(filename) {
  64. _initializeVFS.call(this);
  65. if (typeof this.internal.vFS[filename] !== "undefined") {
  66. return this.internal.vFS[filename];
  67. }
  68. return null;
  69. };
  70. })(jsPDF.API);