plugins_addhtml.js.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>plugins/addhtml.js - Documentation</title>
  6. <script src="scripts/prettify/prettify.js"></script>
  7. <script src="scripts/prettify/lang-css.js"></script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
  13. </head>
  14. <body>
  15. <input type="checkbox" id="nav-trigger" class="nav-trigger" />
  16. <label for="nav-trigger" class="navicon-button x">
  17. <div class="navicon"></div>
  18. </label>
  19. <label for="nav-trigger" class="overlay"></label>
  20. <nav>
  21. <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="jsPDF.html">jsPDF</a></li></ul><h3>Global</h3><ul><li><a href="global.html#addFont">addFont</a></li><li><a href="global.html#addHTML">addHTML</a></li><li><a href="global.html#addMetadata">addMetadata</a></li><li><a href="global.html#addPage">addPage</a></li><li><a href="global.html#autoPrint">autoPrint</a></li><li><a href="global.html#CapJoinStyles">CapJoinStyles</a></li><li><a href="global.html#circle">circle</a></li><li><a href="global.html#ellipse">ellipse</a></li><li><a href="global.html#getFontList">getFontList</a></li><li><a href="global.html#lines">lines</a></li><li><a href="global.html#lstext">lstext</a></li><li><a href="global.html#output">output</a></li><li><a href="global.html#rect">rect</a></li><li><a href="global.html#roundedRect">roundedRect</a></li><li><a href="global.html#save">save</a></li><li><a href="global.html#setDisplayMode">setDisplayMode</a></li><li><a href="global.html#setDrawColor">setDrawColor</a></li><li><a href="global.html#setFillColor">setFillColor</a></li><li><a href="global.html#setFont">setFont</a></li><li><a href="global.html#setFontSize">setFontSize</a></li><li><a href="global.html#setFontStyle">setFontStyle</a></li><li><a href="global.html#setLineCap">setLineCap</a></li><li><a href="global.html#setLineJoin">setLineJoin</a></li><li><a href="global.html#setLineWidth">setLineWidth</a></li><li><a href="global.html#setPage">setPage</a></li><li><a href="global.html#setProperties">setProperties</a></li><li><a href="global.html#setTextColor">setTextColor</a></li><li><a href="global.html#text">text</a></li><li><a href="global.html#triangle">triangle</a></li></ul>
  22. </nav>
  23. <div id="main">
  24. <h1 class="page-title">plugins/addhtml.js</h1>
  25. <section>
  26. <article>
  27. <pre class="prettyprint source linenums"><code>/**
  28. * jsPDF addHTML PlugIn
  29. * Copyright (c) 2014 Diego Casorran
  30. *
  31. * Licensed under the MIT License.
  32. * http://opensource.org/licenses/mit-license
  33. */
  34. (function (jsPDFAPI) {
  35. 'use strict';
  36. /**
  37. * Renders an HTML element to canvas object which added to the PDF
  38. *
  39. * This feature requires [html2canvas](https://github.com/niklasvh/html2canvas)
  40. * or [rasterizeHTML](https://github.com/cburgmer/rasterizeHTML.js)
  41. *
  42. * @returns {jsPDF}
  43. * @name addHTML
  44. * @param element {Mixed} HTML Element, or anything supported by html2canvas.
  45. * @param x {Number} starting X coordinate in jsPDF instance's declared units.
  46. * @param y {Number} starting Y coordinate in jsPDF instance's declared units.
  47. * @param options {Object} Additional options, check the code below.
  48. * @param callback {Function} to call when the rendering has finished.
  49. * NOTE: Every parameter is optional except 'element' and 'callback', in such
  50. * case the image is positioned at 0x0 covering the whole PDF document
  51. * size. Ie, to easily take screenshots of webpages saving them to PDF.
  52. * @deprecated This is being replace with a vector-supporting API. See
  53. * [this link](https://cdn.rawgit.com/MrRio/jsPDF/master/examples/html2pdf/showcase_supported_html.html)
  54. */
  55. jsPDFAPI.addHTML = function (element, x, y, options, callback) {
  56. 'use strict';
  57. if(typeof html2canvas === 'undefined' &amp;&amp; typeof rasterizeHTML === 'undefined')
  58. throw new Error('You need either '
  59. +'https://github.com/niklasvh/html2canvas'
  60. +' or https://github.com/cburgmer/rasterizeHTML.js');
  61. if(typeof x !== 'number') {
  62. options = x;
  63. callback = y;
  64. }
  65. if(typeof options === 'function') {
  66. callback = options;
  67. options = null;
  68. }
  69. var I = this.internal, K = I.scaleFactor, W = I.pageSize.width, H = I.pageSize.height;
  70. options = options || {};
  71. options.onrendered = function(obj) {
  72. x = parseInt(x) || 0;
  73. y = parseInt(y) || 0;
  74. var dim = options.dim || {};
  75. var h = dim.h || 0;
  76. var w = dim.w || Math.min(W,obj.width/K) - x;
  77. var format = 'JPEG';
  78. if(options.format)
  79. format = options.format;
  80. if(obj.height > H &amp;&amp; options.pagesplit) {
  81. var crop = function() {
  82. var cy = 0;
  83. while(1) {
  84. var canvas = document.createElement('canvas');
  85. canvas.width = Math.min(W*K,obj.width);
  86. canvas.height = Math.min(H*K,obj.height-cy);
  87. var ctx = canvas.getContext('2d');
  88. ctx.drawImage(obj,0,cy,obj.width,canvas.height,0,0,canvas.width,canvas.height);
  89. var args = [canvas, x,cy?0:y,canvas.width/K,canvas.height/K, format,null,'SLOW'];
  90. this.addImage.apply(this, args);
  91. cy += canvas.height;
  92. if(cy >= obj.height) break;
  93. this.addPage();
  94. }
  95. callback(w,cy,null,args);
  96. }.bind(this);
  97. if(obj.nodeName === 'CANVAS') {
  98. var img = new Image();
  99. img.onload = crop;
  100. img.src = obj.toDataURL("image/png");
  101. obj = img;
  102. } else {
  103. crop();
  104. }
  105. } else {
  106. var alias = Math.random().toString(35);
  107. var args = [obj, x,y,w,h, format,alias,'SLOW'];
  108. this.addImage.apply(this, args);
  109. callback(w,h,alias,args);
  110. }
  111. }.bind(this);
  112. if(typeof html2canvas !== 'undefined' &amp;&amp; !options.rstz) {
  113. return html2canvas(element, options);
  114. }
  115. if(typeof rasterizeHTML !== 'undefined') {
  116. var meth = 'drawDocument';
  117. if(typeof element === 'string') {
  118. meth = /^http/.test(element) ? 'drawURL' : 'drawHTML';
  119. }
  120. options.width = options.width || (W*K);
  121. return rasterizeHTML[meth](element, void 0, options).then(function(r) {
  122. options.onrendered(r.image);
  123. }, function(e) {
  124. callback(null,e);
  125. });
  126. }
  127. return null;
  128. };
  129. })(jsPDF.API);
  130. </code></pre>
  131. </article>
  132. </section>
  133. </div>
  134. <br class="clear">
  135. <footer>
  136. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.2</a> on Sun Oct 09 2016 11:08:27 GMT+0100 (BST) using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
  137. </footer>
  138. <script>prettyPrint();</script>
  139. <script src="scripts/linenumber.js"></script>
  140. </body>
  141. </html>