|
1 | 1 | /** @preserve
|
2 |
| - * jsPDF fromHTML plugin. BETA stage. API subject to change. Needs browser, jQuery |
| 2 | + * jsPDF fromHTML plugin. BETA stage. API subject to change. Needs browser |
3 | 3 | * Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
|
4 | 4 | * 2014 Juan Pablo Gaviria, https://github.com/juanpgaviria
|
5 | 5 | * 2014 Diego Casorran, https://github.com/diegocr
|
6 |
| - * |
| 6 | + * |
7 | 7 | * Permission is hereby granted, free of charge, to any person obtaining
|
8 | 8 | * a copy of this software and associated documentation files (the
|
9 | 9 | * "Software"), to deal in the Software without restriction, including
|
10 | 10 | * without limitation the rights to use, copy, modify, merge, publish,
|
11 | 11 | * distribute, sublicense, and/or sell copies of the Software, and to
|
12 | 12 | * permit persons to whom the Software is furnished to do so, subject to
|
13 | 13 | * the following conditions:
|
14 |
| - * |
| 14 | + * |
15 | 15 | * The above copyright notice and this permission notice shall be
|
16 | 16 | * included in all copies or substantial portions of the Software.
|
17 |
| - * |
| 17 | + * |
18 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 | 19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20 | 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
117 | 117 | return UnitedNumberMap[css_line_height_string] = 1;
|
118 | 118 | };
|
119 | 119 | GetCSS = function(element) {
|
120 |
| - var $e, css, tmp; |
121 |
| - $e = $(element); |
| 120 | + var css, tmp, computedCSSElement; |
| 121 | + computedCSSElement = (function(el) { |
| 122 | + var compCSS; |
| 123 | + compCSS = (function(el) { |
| 124 | + if ( document.defaultView && document.defaultView.getComputedStyle ) { |
| 125 | + return document.defaultView.getComputedStyle(el, null); |
| 126 | + } else if ( el.currentStyle ) { |
| 127 | + return el.currentStyle; |
| 128 | + } else { |
| 129 | + return el.style; |
| 130 | + } |
| 131 | + })(el); |
| 132 | + return function(prop) { |
| 133 | + prop = prop.replace(/-\D/g, function(match){ |
| 134 | + return match.charAt(1).toUpperCase(); |
| 135 | + }) |
| 136 | + return compCSS[prop]; |
| 137 | + } |
| 138 | + })(element); |
122 | 139 | css = {};
|
123 | 140 | tmp = void 0;
|
124 |
| - css["font-family"] = ResolveFont($e.css("font-family")) || "times"; |
125 |
| - css["font-style"] = FontStyleMap[$e.css("font-style")] || "normal"; |
126 |
| - tmp = FontWeightMap[$e.css("font-weight")] || "normal"; |
| 141 | + css["font-family"] = ResolveFont(computedCSSElement("font-family")) || "times"; |
| 142 | + css["font-style"] = FontStyleMap[computedCSSElement("font-style")] || "normal"; |
| 143 | + tmp = FontWeightMap[computedCSSElement("font-weight")] || "normal"; |
127 | 144 | if (tmp === "bold") {
|
128 | 145 | if (css["font-style"] === "normal") {
|
129 | 146 | css["font-style"] = tmp;
|
130 | 147 | } else {
|
131 | 148 | css["font-style"] = tmp + css["font-style"];
|
132 | 149 | }
|
133 | 150 | }
|
134 |
| - css["font-size"] = ResolveUnitedNumber($e.css("font-size")) || 1; |
135 |
| - css["line-height"] = ResolveUnitedNumber($e.css("line-height")) || 1; |
136 |
| - css["display"] = ($e.css("display") === "inline" ? "inline" : "block"); |
| 151 | + css["font-size"] = ResolveUnitedNumber(computedCSSElement("font-size")) || 1; |
| 152 | + css["line-height"] = ResolveUnitedNumber(computedCSSElement("line-height")) || 1; |
| 153 | + css["display"] = (computedCSSElement("display") === "inline" ? "inline" : "block"); |
137 | 154 | if (css["display"] === "block") {
|
138 |
| - css["margin-top"] = ResolveUnitedNumber($e.css("margin-top")) || 0; |
139 |
| - css["margin-bottom"] = ResolveUnitedNumber($e.css("margin-bottom")) || 0; |
140 |
| - css["padding-top"] = ResolveUnitedNumber($e.css("padding-top")) || 0; |
141 |
| - css["padding-bottom"] = ResolveUnitedNumber($e.css("padding-bottom")) || 0; |
142 |
| - css["margin-left"] = ResolveUnitedNumber($e.css("margin-left")) || 0; |
143 |
| - css["margin-right"] = ResolveUnitedNumber($e.css("margin-right")) || 0; |
144 |
| - css["padding-left"] = ResolveUnitedNumber($e.css("padding-left")) || 0; |
145 |
| - css["padding-right"] = ResolveUnitedNumber($e.css("padding-right")) || 0; |
| 155 | + css["margin-top"] = ResolveUnitedNumber(computedCSSElement("margin-top")) || 0; |
| 156 | + css["margin-bottom"] = ResolveUnitedNumber(computedCSSElement("margin-bottom")) || 0; |
| 157 | + css["padding-top"] = ResolveUnitedNumber(computedCSSElement("padding-top")) || 0; |
| 158 | + css["padding-bottom"] = ResolveUnitedNumber(computedCSSElement("padding-bottom")) || 0; |
| 159 | + css["margin-left"] = ResolveUnitedNumber(computedCSSElement("margin-left")) || 0; |
| 160 | + css["margin-right"] = ResolveUnitedNumber(computedCSSElement("margin-right")) || 0; |
| 161 | + css["padding-left"] = ResolveUnitedNumber(computedCSSElement("padding-left")) || 0; |
| 162 | + css["padding-right"] = ResolveUnitedNumber(computedCSSElement("padding-right")) || 0; |
146 | 163 | }
|
147 | 164 | return css;
|
148 | 165 | };
|
|
306 | 323 | var $frame, $hiddendiv, framename, visuallyhidden;
|
307 | 324 | framename = "jsPDFhtmlText" + Date.now().toString() + (Math.random() * 1000).toFixed(0);
|
308 | 325 | visuallyhidden = "position: absolute !important;" + "clip: rect(1px 1px 1px 1px); /* IE6, IE7 */" + "clip: rect(1px, 1px, 1px, 1px);" + "padding:0 !important;" + "border:0 !important;" + "height: 1px !important;" + "width: 1px !important; " + "top:auto;" + "left:-100px;" + "overflow: hidden;";
|
309 |
| - $hiddendiv = $("<div style=\"" + visuallyhidden + "\">" + "<iframe style=\"height:1px;width:1px\" name=\"" + framename + "\" />" + "</div>").appendTo(document.body); |
| 326 | + $hiddendiv = document.createElement('div'); |
| 327 | + $hiddendiv.style.cssText = visuallyhidden; |
| 328 | + $hiddendiv.innerHTML = "<iframe style=\"height:1px;width:1px\" name=\"" + framename + "\" />"; |
| 329 | + document.body.appendChild($hiddendiv); |
310 | 330 | $frame = window.frames[framename];
|
311 |
| - return $($frame.document.body).html(element)[0]; |
| 331 | + $frame.document.body.innerHTML = element; |
| 332 | + return $frame.document.body; |
312 | 333 | })(element.replace(/<\/?script[^>]*?>/gi,''));
|
313 | 334 | }
|
314 | 335 | var r = new Renderer(pdf, x, y, settings);
|
|
487 | 508 | normal: 1
|
488 | 509 | /*
|
489 | 510 | Converts HTML-formatted text into formatted PDF text.
|
490 |
| - |
| 511 | +
|
491 | 512 | Notes:
|
492 | 513 | 2012-07-18
|
493 | 514 | Plugin relies on having browser, DOM around. The HTML is pushed into dom and traversed.
|
494 | 515 | Plugin relies on jQuery for CSS extraction.
|
495 | 516 | Targeting HTML output from Markdown templating, which is a very simple
|
496 | 517 | markup - div, span, em, strong, p. No br-based paragraph separation supported explicitly (but still may work.)
|
497 | 518 | Images, tables are NOT supported.
|
498 |
| - |
| 519 | +
|
499 | 520 | @public
|
500 | 521 | @function
|
501 | 522 | @param HTML {String or DOM Element} HTML-formatted text, or pointer to DOM element that is to be rendered into PDF.
|
|
0 commit comments