Skip to content

Commit c4dfe00

Browse files
committed
Merge pull request #228 from bolste/master
fromHTML assumes jQuery is available, fix #224
2 parents 27df11e + d5fde52 commit c4dfe00

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

jspdf.plugin.from_html.js

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/** @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
33
* Copyright (c) 2012 Willow Systems Corporation, willow-systems.com
44
* 2014 Juan Pablo Gaviria, https://github.com/juanpgaviria
55
* 2014 Diego Casorran, https://github.com/diegocr
6-
*
6+
*
77
* Permission is hereby granted, free of charge, to any person obtaining
88
* a copy of this software and associated documentation files (the
99
* "Software"), to deal in the Software without restriction, including
1010
* without limitation the rights to use, copy, modify, merge, publish,
1111
* distribute, sublicense, and/or sell copies of the Software, and to
1212
* permit persons to whom the Software is furnished to do so, subject to
1313
* the following conditions:
14-
*
14+
*
1515
* The above copyright notice and this permission notice shall be
1616
* included in all copies or substantial portions of the Software.
17-
*
17+
*
1818
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1919
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2020
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -117,32 +117,49 @@
117117
return UnitedNumberMap[css_line_height_string] = 1;
118118
};
119119
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);
122139
css = {};
123140
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";
127144
if (tmp === "bold") {
128145
if (css["font-style"] === "normal") {
129146
css["font-style"] = tmp;
130147
} else {
131148
css["font-style"] = tmp + css["font-style"];
132149
}
133150
}
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");
137154
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;
146163
}
147164
return css;
148165
};
@@ -306,9 +323,13 @@
306323
var $frame, $hiddendiv, framename, visuallyhidden;
307324
framename = "jsPDFhtmlText" + Date.now().toString() + (Math.random() * 1000).toFixed(0);
308325
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);
310330
$frame = window.frames[framename];
311-
return $($frame.document.body).html(element)[0];
331+
$frame.document.body.innerHTML = element;
332+
return $frame.document.body;
312333
})(element.replace(/<\/?script[^>]*?>/gi,''));
313334
}
314335
var r = new Renderer(pdf, x, y, settings);
@@ -487,15 +508,15 @@
487508
normal: 1
488509
/*
489510
Converts HTML-formatted text into formatted PDF text.
490-
511+
491512
Notes:
492513
2012-07-18
493514
Plugin relies on having browser, DOM around. The HTML is pushed into dom and traversed.
494515
Plugin relies on jQuery for CSS extraction.
495516
Targeting HTML output from Markdown templating, which is a very simple
496517
markup - div, span, em, strong, p. No br-based paragraph separation supported explicitly (but still may work.)
497518
Images, tables are NOT supported.
498-
519+
499520
@public
500521
@function
501522
@param HTML {String or DOM Element} HTML-formatted text, or pointer to DOM element that is to be rendered into PDF.

0 commit comments

Comments
 (0)