Skip to content

Commit 2ea8049

Browse files
authored
Merge pull request #92 from jasonday/canvas
Experimental fix for copying canvas content.
2 parents 2693bb1 + 6f05ec1 commit 2ea8049

5 files changed

Lines changed: 119 additions & 31 deletions

File tree

README.md

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,100 @@ Printing plug-in for jQuery
66
* Preserve page CSS/styling
77
** or add new CSS; the world is your oyster!
88
* Preserve form entries
9-
10-
### Non-features
11-
really 'Issues'
12-
* canvas/svg elements such as map routes are not preserved
9+
* **1.9.0 adds experimental canvas support**
1310

1411
## Usage
1512
### Basic
1613
```javascript
17-
$('selector').printThis();
14+
$('selector').printThis();
15+
```
16+
17+
### Advanced Features
18+
```javascript
19+
$('#kitty-one, #kitty-two, #kitty-three').printThis({
20+
importCSS: false,
21+
loadCSS: "",
22+
header: "<h1>Look at all of my kitties!</h1>"
23+
});
24+
```
25+
26+
#### debug
27+
Debug leaves the iframe visible on the page after `printThis` runs, allowing you to inspect the markup and CSS.
28+
29+
#### importCSS
30+
Copy CSS `<link>` tags to the printThis iframe. On by default.
31+
32+
#### importStyle
33+
Copy CSS `<style>` tags to the printThis iframe. Off by default.
34+
35+
#### printContainer
36+
Includes the markup of the selected container, not just its contents. On by default.
37+
38+
#### loadCSS
39+
Provide a URL for an additional stylesheet to the printThis iframe. Empty string (off) by default.
40+
41+
#### pageTitle
42+
Use a custom page title on the iframe. This may be reflected on the printed page, depending on settings. Blank by default.
43+
44+
#### removeInline
45+
Eliminates any inline style attributes from the content. Off by default.
46+
47+
#### printDelay
48+
The amount of time to wait before calling `print()` in the printThis iframe. Defaults to 333 milliseconds.
49+
Appropriate values depend heavily on the content and network performance. Graphics heavy, slow, or uncached content may need extra time to load.
50+
51+
#### header & footer
52+
A string or jQuery object to prepend or append to the printThis iframe content. `null` by default.
53+
54+
```javascript
55+
$('#mySelector').printThis({
56+
header: "<h1>Amazing header</h1>"
57+
});
58+
59+
$('#mySelector').printThis({
60+
footer: $('.hidden-print-header-content')
61+
});
1862
```
1963

20-
### Advanced
21-
$('#kitty-one, #kitty-two, #kitty-three').printThis({
22-
importCSS: false,
23-
loadCSS: "",
24-
header: "<h1>Look at all of my kitties!</h1>"
25-
});
64+
#### base
65+
The `base` option allows several behaviors.
66+
By default it is `false`, meaning a the current document will be set as the base URL.
67+
68+
If set to `true`, a `<base>` attribute will be set if one exists on the page.
69+
If none is found, the tag is omitted, which may be suitable for pages with Fully Qualified URLs.
70+
71+
When passed as a string, it will be used as the `href` attribute of a `<base>` tag.
72+
73+
#### formValues
74+
This setting attempts to copy the current values of form elements into the printThis iframe. On by default.
75+
76+
Complex field values, such as those containing quotations or mark-up, may have difficulties. Please report any unexpected behavior.
77+
78+
#### canvas -- Experimental
79+
As of 1.9.0 you may be able to duplicate canvas elements to the printThis iframe. Disabled by default.
80+
This has received only limited testing and so may not work in all browsers and situations.
81+
82+
#### doctypeString
83+
A doctype string to use on the printThis iframe. Defaults to the HTML5 doctype.
2684

2785
### All Options
2886
```javascript
29-
$("#mySelector").printThis({
30-
debug: false, * show the iframe for debugging
31-
importCSS: true, * import page CSS
32-
importStyle: false, * import style tags
33-
printContainer: true, * grab outer container as well as the contents of the selector
34-
loadCSS: "path/to/my.css", * path to additional css file - use an array [] for multiple
35-
pageTitle: "", * add title to print page
36-
removeInline: false, * remove all inline styles from print elements
37-
printDelay: 333, * variable print delay; depending on complexity a higher value may be necessary
38-
header: null, * prefix to html
39-
base: false * preserve the BASE tag, or accept a string for the URL
40-
formValues: true * preserve input/form values
41-
});
87+
$("#mySelector").printThis({
88+
debug: false, * show the iframe for debugging
89+
importCSS: true, * import page CSS
90+
importStyle: false, * import style tags
91+
printContainer: true, * grab outer container as well as the contents of the selector
92+
loadCSS: "path/to/my.css", * path to additional css file - use an array [] for multiple
93+
pageTitle: "", * add title to print page
94+
removeInline: false, * remove all inline styles from print elements
95+
printDelay: 333, * variable print delay; depending on complexity a higher value may be necessary
96+
header: null, * prefix to html
97+
footer: null, * postfix to html
98+
base: false * preserve the BASE tag, or accept a string for the URL
99+
formValues: true * preserve input/form values
100+
canvas: false * copy canvas elements (experimental)
101+
doctypeString: "..." * enter a different doctype for older markup
102+
});
42103
```
43104

44105
## Please read

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
01/16/2017 Added experimental canvas copy support.
2+
Added detailed descriptions of advanced options to README
3+
Bumped to 1.9.0 for new feature (canvas).
4+
15
01/16/2017 Added footer as proposed in 2015 by @RomainGehrig
26
Bumped version to 1.8.0 for new feature
37
Corrected 1.7.1 changelog date to 01/15/2017

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "print-this",
3-
"version": "1.8.0",
3+
"version": "1.9.0",
44
"description": "Printing plug-in for jQuery",
55
"main": "printThis.js",
66
"dependencies": {

printThis.jquery.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "printThis",
3-
"version": "1.8.0",
3+
"version": "1.9.0",
44
"title": "printThis",
55
"description": "Printing plug-in for jQuery. Print specific page elements, add print options, maintain or add new styling using jQuery.",
66
"author": {

printThis.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* printThis v1.8.0
2+
* printThis v1.9.0
33
* @desc Printing plug-in for jQuery
44
* @author Jason Day
55
*
@@ -28,6 +28,8 @@
2828
* footer: null, * postfix to html
2929
* base: false, * preserve the BASE tag, or accept a string for the URL
3030
* formValues: true * preserve input/form values
31+
* canvas: false * copy canvas elements (experimental)
32+
* doctypeString: '...' * enter a different doctype for older markup
3133
* });
3234
*
3335
* Notes:
@@ -45,7 +47,7 @@
4547
if (window.location.hostname !== document.domain && navigator.userAgent.match(/msie/i)) {
4648
// Ugly IE hacks due to IE not inheriting document.domain from parent
4749
// checks if document.domain is set by comparing the host name against document.domain
48-
var iframeSrc = "javascript:document.write(\"<head><script>document.domain=\\\"" + document.domain + "\\\";</script></head><body></body>\")";
50+
var iframeSrc = "javascript:document.write(\"<head><script>document.domain=\\\"" + document.domain + "\\\";</s" + "cript></head><body></body>\")";
4951
var printI = document.createElement('iframe');
5052
printI.name = "printIframe";
5153
printI.id = strFrameName;
@@ -71,7 +73,6 @@
7173
top: "-600px"
7274
});
7375

74-
7576
// $iframe.ready() and $iframe.load were inconsistent between browsers
7677
setTimeout(function() {
7778

@@ -140,6 +141,14 @@
140141
// print header
141142
if (opt.header) $body.append(opt.header);
142143

144+
if (opt.canvas) {
145+
// add canvas data-ids for easy access after the cloning.
146+
var canvasId = 0;
147+
$element.find('canvas').each(function(){
148+
$(this).attr('data-printthis', canvasId++);
149+
});
150+
}
151+
143152
// grab $.selector as container
144153
if (opt.printContainer) $body.append($element.outer());
145154

@@ -148,6 +157,19 @@
148157
$body.append($(this).html());
149158
});
150159

160+
if (opt.canvas) {
161+
// Re-draw new canvases by referencing the originals
162+
$body.find('canvas').each(function(){
163+
var cid = $(this).data('printthis'),
164+
$src = $('[data-printthis="' + cid + '"]');
165+
166+
this.getContext('2d').drawImage($src[0], 0, 0);
167+
168+
// Remove the mark-up from the original
169+
$src.removeData('printthis');
170+
});
171+
}
172+
151173
// capture form/field values
152174
if (opt.formValues) {
153175
// loop through inputs
@@ -215,7 +237,7 @@
215237
// check if the iframe was created with the ugly hack
216238
// and perform another ugly hack out of neccessity
217239
window.frames["printIframe"].focus();
218-
$head.append("<script> window.print(); </script>");
240+
$head.append("<script> window.print(); </s" + "cript>");
219241
} else {
220242
// proper method
221243
if (document.queryCommandSupported("print")) {
@@ -252,12 +274,13 @@
252274
header: null, // prefix to html
253275
footer: null, // postfix to html
254276
formValues: true, // preserve input/form values
277+
canvas: false, // Copy canvas content (experimental)
255278
base: false, // preserve the BASE tag, or accept a string for the URL
256279
doctypeString: '<!DOCTYPE html>' // html doctype
257280
};
258281

259282
// $.selector container
260283
jQuery.fn.outer = function() {
261-
return $($("<div></div>").html(this.clone())).html()
284+
return $($("<div></div>").html(this.clone())).html();
262285
}
263286
})(jQuery);

0 commit comments

Comments
 (0)