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.wordexport.js 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // Create canvas for converting image to data URL
  31. var canvas = document.createElement("CANVAS");
  32. canvas.width = w;
  33. canvas.height = h;
  34. // Draw image to canvas
  35. var context = canvas.getContext('2d');
  36. context.drawImage(img[i], 0, 0, w, h);
  37. // Get data URL encoding of image
  38. var uri = canvas.toDataURL("image/png");
  39. $(img[i]).attr("src", img[i].src);
  40. img[i].width = w;
  41. img[i].height = h;
  42. // Save encoded image to array
  43. images[i] = {
  44. type: uri.substring(uri.indexOf(":") + 1, uri.indexOf(";")),
  45. encoding: uri.substring(uri.indexOf(";") + 1, uri.indexOf(",")),
  46. location: $(img[i]).attr("src"),
  47. data: uri.substring(uri.indexOf(",") + 1)
  48. };
  49. }
  50. // Prepare bottom of mhtml file with image data
  51. var mhtmlBottom = "\n";
  52. for (var i = 0; i < images.length; i++) {
  53. mhtmlBottom += "--NEXT.ITEM-BOUNDARY\n";
  54. mhtmlBottom += "Content-Location: " + images[i].location + "\n";
  55. mhtmlBottom += "Content-Type: " + images[i].type + "\n";
  56. mhtmlBottom += "Content-Transfer-Encoding: " + images[i].encoding + "\n\n";
  57. mhtmlBottom += images[i].data + "\n\n";
  58. }
  59. mhtmlBottom += "--NEXT.ITEM-BOUNDARY--";
  60. //TODO: load css from included stylesheet
  61. var styles = "";
  62. // Aggregate parts of the file together
  63. var fileContent = static.mhtml.top.replace("_html_", static.mhtml.head.replace("_styles_", styles) + static.mhtml.body.replace("_body_", markup.html())) + mhtmlBottom;
  64. // Create a Blob with the file contents
  65. var blob = new Blob([fileContent], {
  66. type: "application/msword;charset=utf-8"
  67. });
  68. saveAs(blob, fileName + ".doc");
  69. };
  70. })(jQuery);
  71. } else {
  72. if (typeof jQuery === "undefined") {
  73. console.error("jQuery Word Export: missing dependency (jQuery)");
  74. }
  75. if (typeof saveAs === "undefined") {
  76. console.error("jQuery Word Export: missing dependency (FileSaver.js)");
  77. }
  78. }