modules/xmp_metadata.js

  1. /** ====================================================================
  2. * @license
  3. * jsPDF XMP metadata plugin
  4. * Copyright (c) 2016 Jussi Utunen, u-jussi@suomi24.fi
  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. * @name xmp_metadata
  29. * @module
  30. */
  31. (function(jsPDFAPI) {
  32. "use strict";
  33. var postPutResources = function() {
  34. var xmpmeta_beginning = '<x:xmpmeta xmlns:x="adobe:ns:meta/">';
  35. var rdf_beginning =
  36. '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><rdf:Description rdf:about="" xmlns:jspdf="' +
  37. this.internal.__metadata__.namespaceuri +
  38. '"><jspdf:metadata>';
  39. var rdf_ending = "</jspdf:metadata></rdf:Description></rdf:RDF>";
  40. var xmpmeta_ending = "</x:xmpmeta>";
  41. var utf8_xmpmeta_beginning = unescape(
  42. encodeURIComponent(xmpmeta_beginning)
  43. );
  44. var utf8_rdf_beginning = unescape(encodeURIComponent(rdf_beginning));
  45. var utf8_metadata = unescape(
  46. encodeURIComponent(this.internal.__metadata__.metadata)
  47. );
  48. var utf8_rdf_ending = unescape(encodeURIComponent(rdf_ending));
  49. var utf8_xmpmeta_ending = unescape(encodeURIComponent(xmpmeta_ending));
  50. var total_len =
  51. utf8_rdf_beginning.length +
  52. utf8_metadata.length +
  53. utf8_rdf_ending.length +
  54. utf8_xmpmeta_beginning.length +
  55. utf8_xmpmeta_ending.length;
  56. this.internal.__metadata__.metadata_object_number = this.internal.newObject();
  57. this.internal.write(
  58. "<< /Type /Metadata /Subtype /XML /Length " + total_len + " >>"
  59. );
  60. this.internal.write("stream");
  61. this.internal.write(
  62. utf8_xmpmeta_beginning +
  63. utf8_rdf_beginning +
  64. utf8_metadata +
  65. utf8_rdf_ending +
  66. utf8_xmpmeta_ending
  67. );
  68. this.internal.write("endstream");
  69. this.internal.write("endobj");
  70. };
  71. var putCatalog = function() {
  72. if (this.internal.__metadata__.metadata_object_number) {
  73. this.internal.write(
  74. "/Metadata " +
  75. this.internal.__metadata__.metadata_object_number +
  76. " 0 R"
  77. );
  78. }
  79. };
  80. /**
  81. * Adds XMP formatted metadata to PDF
  82. *
  83. * @name addMetadata
  84. * @function
  85. * @param {String} metadata The actual metadata to be added. The metadata shall be stored as XMP simple value. Note that if the metadata string contains XML markup characters "<", ">" or "&", those characters should be written using XML entities.
  86. * @param {String} namespaceuri Sets the namespace URI for the metadata. Last character should be slash or hash.
  87. * @returns {jsPDF} jsPDF-instance
  88. */
  89. jsPDFAPI.addMetadata = function(metadata, namespaceuri) {
  90. if (typeof this.internal.__metadata__ === "undefined") {
  91. this.internal.__metadata__ = {
  92. metadata: metadata,
  93. namespaceuri: namespaceuri || "http://jspdf.default.namespaceuri/"
  94. };
  95. this.internal.events.subscribe("putCatalog", putCatalog);
  96. this.internal.events.subscribe("postPutResources", postPutResources);
  97. }
  98. return this;
  99. };
  100. })(jsPDF.API);