Sin descripción

jquery.wordexport.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. if (typeof jQuery !== "undefined" && typeof saveAs !== "undefined") {
  2. (function($) {
  3. $.fn.wordExport = function(fileName) {
  4. fileName = typeof fileName !== 'undefined' ? fileName : "jQuery-Word-Export";
  5. var static = {
  6. mhtml: {
  7. top: "Mime-Version: 1.0\nContent-Base: " + location.href + "\nContent-Type: Multipart/related; boundary=\"NEXT.ITEM-BOUNDARY\";type=\"text/html\"\n\n--NEXT.ITEM-BOUNDARY\nContent-Type: text/html; charset=\"utf-8\"\nContent-Location: " + location.href + "\n\n<!DOCTYPE html>\n<html>\n_html_</html>",
  8. head: "<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n<style>\n_styles_\n</style>\n</head>\n",
  9. body: "<body>_body_</body>"
  10. }
  11. };
  12. var options = {
  13. maxWidth: 624,
  14. width:500,
  15. height:500,
  16. };
  17. // Clone selected element before manipulating it
  18. var markup = $(this).clone();
  19. // Remove hidden elements from the output
  20. markup.each(function() {
  21. var self = $(this);
  22. if (self.is(':hidden'))
  23. self.remove();
  24. });
  25. // Embed all images using Data URLs
  26. var images = Array();
  27. var img = markup.find('img');
  28. for (var i = 0; i < img.length; i++) {
  29. // Calculate dimensions of output image
  30. var w = Math.min(img[i].width, options.maxWidth);
  31. var h = img[i].height * (w / img[i].width);
  32. // Create canvas for converting image to data URL
  33. var canvas = document.createElement("CANVAS");
  34. canvas.width = w;
  35. canvas.height = h;
  36. // Draw image to canvas
  37. var context = canvas.getContext('2d');
  38. context.drawImage(img[i], 0, 0, w, h);
  39. // Get data URL encoding of image
  40. var uri = canvas.toDataURL("image/png");
  41. $(img[i]).attr("src", img[i].src);
  42. img[i].width = w;
  43. img[i].height = h;
  44. // Save encoded image to array
  45. images[i] = {
  46. type: uri.substring(uri.indexOf(":") + 1, uri.indexOf(";")),
  47. encoding: uri.substring(uri.indexOf(";") + 1, uri.indexOf(",")),
  48. location: $(img[i]).attr("src"),
  49. data: uri.substring(uri.indexOf(",") + 1)
  50. };
  51. }
  52. // Prepare bottom of mhtml file with image data
  53. var mhtmlBottom = "\n";
  54. for (var i = 0; i < images.length; i++) {
  55. mhtmlBottom += "--NEXT.ITEM-BOUNDARY\n";
  56. mhtmlBottom += "Content-Location: " + images[i].location + "\n";
  57. mhtmlBottom += "Content-Type: " + images[i].type + "\n";
  58. mhtmlBottom += "Content-Transfer-Encoding: " + images[i].encoding + "\n\n";
  59. mhtmlBottom += images[i].data + "\n\n";
  60. }
  61. mhtmlBottom += "--NEXT.ITEM-BOUNDARY--";
  62. //TODO: load css from included stylesheet
  63. var styles = "";
  64. // Aggregate parts of the file together
  65. var fileContent = static.mhtml.top.replace("_html_", static.mhtml.head.replace("_styles_", styles) + static.mhtml.body.replace("_body_", markup.html())) + mhtmlBottom;
  66. // Create a Blob with the file contents
  67. var blob = new Blob([fileContent], {
  68. type: "application/msword;charset=utf-8"
  69. });
  70. saveAs(blob, fileName + ".doc");
  71. };
  72. })(jQuery);
  73. } else {
  74. if (typeof jQuery === "undefined") {
  75. console.error("jQuery Word Export: missing dependency (jQuery)");
  76. }
  77. if (typeof saveAs === "undefined") {
  78. console.error("jQuery Word Export: missing dependency (FileSaver.js)");
  79. }
  80. }