You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jQuery.print.js 9.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* @license
  2. * jQuery.print, version 1.5.1
  3. * (c) Sathvik Ponangi, Doers' Guild
  4. * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
  5. *--------------------------------------------------------------------------*/
  6. (function ($) {
  7. "use strict";
  8. // A nice closure for our definitions
  9. function getjQueryObject(string) {
  10. // Make string a vaild jQuery thing
  11. var jqObj = $("");
  12. try {
  13. jqObj = $(string)
  14. .clone();
  15. } catch (e) {
  16. jqObj = $("<span />")
  17. .html(string);
  18. }
  19. return jqObj;
  20. }
  21. function printFrame(frameWindow, content, options) {
  22. // Print the selected window/iframe
  23. var def = $.Deferred();
  24. try {
  25. frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow;
  26. var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow;
  27. if(options.doctype) {
  28. wdoc.write(options.doctype);
  29. }
  30. wdoc.write(content);
  31. wdoc.close();
  32. var printed = false;
  33. var callPrint = function () {
  34. if(printed) {
  35. return;
  36. }
  37. // Fix for IE : Allow it to render the iframe
  38. frameWindow.focus();
  39. try {
  40. // Fix for IE11 - printng the whole page instead of the iframe content
  41. if (!frameWindow.document.execCommand('print', false, null)) {
  42. // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891
  43. frameWindow.print();
  44. }
  45. // focus body as it is losing focus in iPad and content not getting printed
  46. $('body').focus();
  47. } catch (e) {
  48. frameWindow.print();
  49. }
  50. frameWindow.close();
  51. printed = true;
  52. def.resolve();
  53. }
  54. // Print once the frame window loads - seems to work for the new-window option but unreliable for the iframe
  55. $(frameWindow).on("load", callPrint);
  56. // Fallback to printing directly if the frame doesn't fire the load event for whatever reason
  57. setTimeout(callPrint, options.timeout);
  58. } catch (err) {
  59. def.reject(err);
  60. }
  61. return def;
  62. }
  63. function printContentInIFrame(content, options) {
  64. var $iframe = $(options.iframe + "");
  65. var iframeCount = $iframe.length;
  66. if (iframeCount === 0) {
  67. // Create a new iFrame if none is given
  68. $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
  69. .prependTo('body')
  70. .css({
  71. "position": "absolute",
  72. "top": -999,
  73. "left": -999
  74. });
  75. }
  76. var frameWindow = $iframe.get(0);
  77. return printFrame(frameWindow, content, options)
  78. .done(function () {
  79. // Success
  80. setTimeout(function () {
  81. // Wait for IE
  82. if (iframeCount === 0) {
  83. // Destroy the iframe if created here
  84. $iframe.remove();
  85. }
  86. }, 1000);
  87. })
  88. .fail(function (err) {
  89. // Use the pop-up method if iframe fails for some reason
  90. console.error("Failed to print from iframe", err);
  91. printContentInNewWindow(content, options);
  92. })
  93. .always(function () {
  94. try {
  95. options.deferred.resolve();
  96. } catch (err) {
  97. console.warn('Error notifying deferred', err);
  98. }
  99. });
  100. }
  101. function printContentInNewWindow(content, options) {
  102. // Open a new window and print selected content
  103. var frameWindow = window.open();
  104. return printFrame(frameWindow, content, options)
  105. .always(function () {
  106. try {
  107. options.deferred.resolve();
  108. } catch (err) {
  109. console.warn('Error notifying deferred', err);
  110. }
  111. });
  112. }
  113. function isNode(o) {
  114. /* http://stackoverflow.com/a/384380/937891 */
  115. return !!(typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
  116. }
  117. $.print = $.fn.print = function () {
  118. // Print a given set of elements
  119. var options, $this, self = this;
  120. // console.log("Printing", this, arguments);
  121. if (self instanceof $) {
  122. // Get the node if it is a jQuery object
  123. self = self.get(0);
  124. }
  125. if (isNode(self)) {
  126. // If `this` is a HTML element, i.e. for
  127. // $(selector).print()
  128. $this = $(self);
  129. if (arguments.length > 0) {
  130. options = arguments[0];
  131. }
  132. } else {
  133. if (arguments.length > 0) {
  134. // $.print(selector,options)
  135. $this = $(arguments[0]);
  136. if (isNode($this[0])) {
  137. if (arguments.length > 1) {
  138. options = arguments[1];
  139. }
  140. } else {
  141. // $.print(options)
  142. options = arguments[0];
  143. $this = $("html");
  144. }
  145. } else {
  146. // $.print()
  147. $this = $("html");
  148. }
  149. }
  150. // Default options
  151. var defaults = {
  152. globalStyles: true,
  153. mediaPrint: false,
  154. stylesheet: null,
  155. noPrintSelector: ".no-print",
  156. iframe: true,
  157. append: null,
  158. prepend: null,
  159. manuallyCopyFormValues: true,
  160. deferred: $.Deferred(),
  161. timeout: 750,
  162. title: null,
  163. doctype: '<!doctype html>'
  164. };
  165. // Merge with user-options
  166. options = $.extend({}, defaults, (options || {}));
  167. var $styles = $("");
  168. if (options.globalStyles) {
  169. // Apply the stlyes from the current sheet to the printed page
  170. $styles = $("style, link, meta, base, title");
  171. } else if (options.mediaPrint) {
  172. // Apply the media-print stylesheet
  173. $styles = $("link[media=print]");
  174. }
  175. if (options.stylesheet) {
  176. // Add a custom stylesheet if given
  177. $styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
  178. }
  179. // Create a copy of the element to print
  180. var copy = $this.clone();
  181. // Wrap it in a span to get the HTML markup string
  182. copy = $("<span/>")
  183. .append(copy);
  184. // Remove unwanted elements
  185. copy.find(options.noPrintSelector)
  186. .remove();
  187. // Add in the styles
  188. copy.append($styles.clone());
  189. // Update title
  190. if (options.title) {
  191. var title = $("title", copy);
  192. if (title.length === 0) {
  193. title = $("<title />");
  194. copy.append(title);
  195. }
  196. title.text(options.title);
  197. }
  198. // Appedned content
  199. copy.append(getjQueryObject(options.append));
  200. // Prepended content
  201. copy.prepend(getjQueryObject(options.prepend));
  202. if (options.manuallyCopyFormValues) {
  203. // Manually copy form values into the HTML for printing user-modified input fields
  204. // http://stackoverflow.com/a/26707753
  205. copy.find("input")
  206. .each(function () {
  207. var $field = $(this);
  208. if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
  209. if ($field.prop("checked")) {
  210. $field.attr("checked", "checked");
  211. }
  212. } else {
  213. $field.attr("value", $field.val());
  214. }
  215. });
  216. copy.find("select").each(function () {
  217. var $field = $(this);
  218. $field.find(":selected").attr("selected", "selected");
  219. });
  220. copy.find("textarea").each(function () {
  221. // Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589
  222. var $field = $(this);
  223. $field.text($field.val());
  224. });
  225. }
  226. // Get the HTML markup string
  227. var content = copy.html();
  228. // Notify with generated markup & cloned elements - useful for logging, etc
  229. try {
  230. options.deferred.notify('generated_markup', content, copy);
  231. } catch (err) {
  232. console.warn('Error notifying deferred', err);
  233. }
  234. // Destroy the copy
  235. copy.remove();
  236. if (options.iframe) {
  237. // Use an iframe for printing
  238. try {
  239. printContentInIFrame(content, options);
  240. } catch (e) {
  241. // Use the pop-up method if iframe fails for some reason
  242. console.error("Failed to print from iframe", e.stack, e.message);
  243. printContentInNewWindow(content, options);
  244. }
  245. } else {
  246. // Use a new window for printing
  247. printContentInNewWindow(content, options);
  248. }
  249. return this;
  250. };
  251. })(jQuery);