modules/svg.js

  1. /** @license
  2. * Copyright (c) 2012 Willow Systems Corporation, https://github.com/willowsystems
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. * ====================================================================
  23. */
  24. import { jsPDF } from "../jspdf.js";
  25. import { console } from "../libs/console.js";
  26. import { globalObject } from "../libs/globalObject.js";
  27. /**
  28. * jsPDF SVG plugin
  29. *
  30. * @name svg
  31. * @module
  32. */
  33. (function(jsPDFAPI) {
  34. "use strict";
  35. function loadCanvg() {
  36. return (function() {
  37. if (globalObject["canvg"]) {
  38. return Promise.resolve(globalObject["canvg"]);
  39. }
  40. // @if MODULE_FORMAT='es'
  41. return import("canvg");
  42. // @endif
  43. // @if MODULE_FORMAT!='es'
  44. if (typeof exports === "object" && typeof module !== "undefined") {
  45. return new Promise(function(resolve, reject) {
  46. try {
  47. resolve(require("canvg"));
  48. } catch (e) {
  49. reject(e);
  50. }
  51. });
  52. }
  53. if (typeof define === "function" && define.amd) {
  54. return new Promise(function(resolve, reject) {
  55. try {
  56. require(["canvg"], resolve);
  57. } catch (e) {
  58. reject(e);
  59. }
  60. });
  61. }
  62. return Promise.reject(new Error("Could not load canvg"));
  63. // @endif
  64. })()
  65. .catch(function(e) {
  66. return Promise.reject(new Error("Could not load canvg: " + e));
  67. })
  68. .then(function(canvg) {
  69. return canvg.default ? canvg.default : canvg;
  70. });
  71. }
  72. /**
  73. * Parses SVG XML and saves it as image into the PDF.
  74. *
  75. * Depends on canvas-element and canvg
  76. *
  77. * @name addSvgAsImage
  78. * @public
  79. * @function
  80. * @param {string} SVG-Data as Text
  81. * @param {number} x Coordinate (in units declared at inception of PDF document) against left edge of the page
  82. * @param {number} y Coordinate (in units declared at inception of PDF document) against upper edge of the page
  83. * @param {number} width of SVG-Image (in units declared at inception of PDF document)
  84. * @param {number} height of SVG-Image (in units declared at inception of PDF document)
  85. * @param {string} alias of SVG-Image (if used multiple times)
  86. * @param {string} compression of the generated JPEG, can have the values 'NONE', 'FAST', 'MEDIUM' and 'SLOW'
  87. * @param {number} rotation of the image in degrees (0-359)
  88. *
  89. * @returns jsPDF jsPDF-instance
  90. */
  91. jsPDFAPI.addSvgAsImage = function(
  92. svg,
  93. x,
  94. y,
  95. w,
  96. h,
  97. alias,
  98. compression,
  99. rotation
  100. ) {
  101. if (isNaN(x) || isNaN(y)) {
  102. console.error("jsPDF.addSvgAsImage: Invalid coordinates", arguments);
  103. throw new Error("Invalid coordinates passed to jsPDF.addSvgAsImage");
  104. }
  105. if (isNaN(w) || isNaN(h)) {
  106. console.error("jsPDF.addSvgAsImage: Invalid measurements", arguments);
  107. throw new Error(
  108. "Invalid measurements (width and/or height) passed to jsPDF.addSvgAsImage"
  109. );
  110. }
  111. var canvas = document.createElement("canvas");
  112. canvas.width = w;
  113. canvas.height = h;
  114. var ctx = canvas.getContext("2d");
  115. ctx.fillStyle = "#fff"; /// set white fill style
  116. ctx.fillRect(0, 0, canvas.width, canvas.height);
  117. var options = {
  118. ignoreMouse: true,
  119. ignoreAnimation: true,
  120. ignoreDimensions: true
  121. };
  122. var doc = this;
  123. return loadCanvg()
  124. .then(
  125. function(canvg) {
  126. return canvg.fromString(ctx, svg, options);
  127. },
  128. function() {
  129. return Promise.reject(new Error("Could not load canvg."));
  130. }
  131. )
  132. .then(function(instance) {
  133. return instance.render(options);
  134. })
  135. .then(function() {
  136. doc.addImage(
  137. canvas.toDataURL("image/jpeg", 1.0),
  138. x,
  139. y,
  140. w,
  141. h,
  142. compression,
  143. rotation
  144. );
  145. });
  146. };
  147. })(jsPDF.API);