jquery.wordexport.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. };
  15. // Clone selected element before manipulating it
  16. var markup = $(this).clone();
  17. // Remove hidden elements from the output
  18. markup.each(function () {
  19. var self = $(this);
  20. if (self.is(':hidden'))
  21. self.remove();
  22. });
  23. // Embed all images using Data URLs
  24. var images = Array();
  25. var img = markup.find('img');
  26. for (var i = 0; i < img.length; i++) {
  27. // Calculate dimensions of output image
  28. var w = Math.min(img[i].width, options.maxWidth);
  29. var h = img[i].height * (w / img[i].width);
  30. var img_id = "#" + img[i].id;
  31. $('<canvas>').attr("id", "test_word_img_" + i).width(w).height(h).insertAfter(img_id);
  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 {123
  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. }