Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

printd

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

printd - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

18

index.d.ts
export declare function createStyle(doc: Document, cssText: string): HTMLStyleElement;
export declare function createIFrame(parent?: HTMLElement): HTMLIFrameElement;
export declare type PrintdCallback = (win: Window, doc: Document, node: HTMLElement, launchPrint: Function) => void;
export declare type PrintdLaunchPrint = (window: Window) => void;
export interface PrintdCallbackArgs {
/** Iframe contentWindow reference */
window: Window;
/** Iframe contentDocument reference */
document: Document;
/** HTMLElement copy reference */
element: HTMLElement;
/** Launch the printing dialog */
launchPrint: PrintdLaunchPrint;
}
export declare type PrintdCallback = (args: PrintdCallbackArgs) => void;
export default class Printd {
private parent;
private node;
private iframe;
private readonly parent;
private readonly iframe;
constructor(parent?: HTMLElement);

@@ -9,0 +19,0 @@ getIFrame(): HTMLIFrameElement;

@@ -26,3 +26,2 @@ "use strict";

if (parent === void 0) { parent = window.document.body; }
this.node = null;
this.parent = parent;

@@ -35,17 +34,23 @@ this.iframe = createIFrame(this.parent);

Printd.prototype.print = function (el, cssText, callback) {
this.node = el.cloneNode(true);
var _this = this;
var _a = this.iframe, contentDocument = _a.contentDocument, contentWindow = _a.contentWindow;
if (contentDocument && contentWindow) {
if (cssText) {
contentDocument.head.appendChild(createStyle(contentDocument, cssText));
if (!contentDocument || !contentWindow)
return;
if (cssText) {
contentDocument.head.appendChild(createStyle(contentDocument, cssText));
}
var elCopy = el.cloneNode(true);
if (elCopy) {
contentDocument.body.innerHTML = '';
contentDocument.body.appendChild(elCopy);
if (callback) {
callback({
window: contentWindow,
document: contentDocument,
element: elCopy,
launchPrint: function () { return _this.launchPrint(contentWindow); }
});
}
if (this.node) {
contentDocument.body.innerHTML = '';
contentDocument.body.appendChild(this.node);
if (callback) {
callback(contentWindow, contentDocument, this.node, this.launchPrint);
}
else {
this.launchPrint(contentWindow);
}
else {
this.launchPrint(contentWindow);
}

@@ -52,0 +57,0 @@ }

{
"name": "printd",
"version": "1.0.1",
"version": "1.1.0",
"description": "Print HTML elements in modern browsers.",

@@ -42,2 +42,3 @@ "repository": "joseluisq/printd",

"@types/node": "^8.0.26",
"git-testing-hook": "^0.3.0",
"jasmine-core": "^3.2.1",

@@ -55,5 +56,5 @@ "jasmine-spec-reporter": "^4.2.1",

"tslint": "^5.11.0",
"tslint-config-standard-plus": "^2.0.1",
"tslint-config-standard-plus": "^2.1.3",
"typescript": "^2.9.2"
}
}

@@ -1,1 +0,1 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.printd={})}(this,function(t){"use strict";function e(t,e){const n=t.createElement("style");return n.type="text/css",n.appendChild(window.document.createTextNode(e)),n}function n(t=window.document.body){const e=window.document.createElement("iframe");return e.setAttribute("src","about:blank"),e.setAttribute("style","visibility:hidden;width:0;height:0;position:absolute;z-index:-9999;bottom:0;"),e.setAttribute("width","0"),e.setAttribute("height","0"),e.setAttribute("wmode","opaque"),t.appendChild(e),e}class i{constructor(t=window.document.body){this.node=null,this.parent=t,this.iframe=n(this.parent)}getIFrame(){return this.iframe}print(t,n,i){this.node=t.cloneNode(!0);const{contentDocument:o,contentWindow:d}=this.iframe;o&&d&&(n&&o.head.appendChild(e(o,n)),this.node&&(o.body.innerHTML="",o.body.appendChild(this.node),i?i(d,o,this.node,this.launchPrint):this.launchPrint(d)))}launchPrint(t){t.document.execCommand("print",!1,null)||t.print()}}t.createStyle=e,t.createIFrame=n,t.default=i,t.Printd=i,Object.defineProperty(t,"__esModule",{value:!0})});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.printd={})}(this,function(t){"use strict";function e(t,e){const n=t.createElement("style");return n.type="text/css",n.appendChild(window.document.createTextNode(e)),n}function n(t=window.document.body){const e=window.document.createElement("iframe");return e.setAttribute("src","about:blank"),e.setAttribute("style","visibility:hidden;width:0;height:0;position:absolute;z-index:-9999;bottom:0;"),e.setAttribute("width","0"),e.setAttribute("height","0"),e.setAttribute("wmode","opaque"),t.appendChild(e),e}class i{constructor(t=window.document.body){this.parent=t,this.iframe=n(this.parent)}getIFrame(){return this.iframe}print(t,n,i){const{contentDocument:o,contentWindow:d}=this.iframe;if(!o||!d)return;n&&o.head.appendChild(e(o,n));const r=t.cloneNode(!0);r&&(o.body.innerHTML="",o.body.appendChild(r),i?i({window:d,document:o,element:r,launchPrint:()=>this.launchPrint(d)}):this.launchPrint(d))}launchPrint(t){t.document.execCommand("print",!1,null)||t.print()}}t.createStyle=e,t.createIFrame=n,t.default=i,t.Printd=i,Object.defineProperty(t,"__esModule",{value:!0})});

@@ -5,4 +5,13 @@ # Printd [![npm](https://img.shields.io/npm/v/printd.svg)](https://www.npmjs.com/package/printd) [![npm](https://img.shields.io/npm/dt/printd.svg)](https://www.npmjs.com/package/printd) [![Build Status](https://travis-ci.org/joseluisq/printd.svg?branch=master)](https://travis-ci.org/joseluisq/printd) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

Printd is a small (`618 bytes` gzipped) script to print HTMLElements. Printd opens [the Print Dialog](https://developer.mozilla.org/en-US/docs/Web/API/Window/print) to print elements inside a blank document. It also supports [CSS Text](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for custom styles.
Printd opens your [Browser Print Dialog](https://developer.mozilla.org/en-US/docs/Web/API/Window/print) to print HTML elements inside a blank document.
## Features
- Written and tested entirely in [Typescript](./src/index.ts).
- Tiny script (around `600 bytes` gzipped with no dependencies).
- Print any element **_without_** opening a new window.
- Custom [CSS Text](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) support.
## Demos
- :rocket: [Live demo](https://codepen.io/joseluisq/full/VzRpGb/)

@@ -16,3 +25,3 @@ - :books: [Demo source code](https://github.com/joseluisq/printd-vue-component-example)

```sh
yarn add printd --dev
yarn add printd
```

@@ -23,3 +32,3 @@

```sh
npm install printd --save-dev
npm install printd --save
```

@@ -76,8 +85,6 @@

### Methods
### constructor
#### constructor
Constructor supports an optional parent element (`HTMLElement`) where the printable element will be appended. Default value is `window.document.body`.
The constructor supports an optional parent element (`HTMLElement`) where the printable element will be appended. Default value is `window.document.body`.
Example:

@@ -89,15 +96,22 @@

#### print
### print
Function to print the current `HTMLElement`.
Params:
```ts
d.print (el, cssText, callback)
```
- __el:__ The `HTMLElement` to print.
__Print parameters:__
- __element:__ The `HTMLElement` to print.
- __cssText:__ Optional [CSS Text](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) to add custom styles to the current element.
- __callback:__ Optional callback function. _Inside the callback it's necessary to call `launchPrint(win)` to trigger the printing._
- __win__: `Window` reference.
- __doc__: `Document` reference.
- __node__: `HTMLElement` reference.
- __launchPrint(win)__: `Function` to trigger the printing on demand.
- __callback:__ Optional callback function. It's necessary to call `launchPrint()` to trigger the printing.
__Print callback arguments:__
- __document__: Iframe `contentDocument` reference.
- __window__: Iframe `contentWindow` reference
- __element__: `HTMLElement` copy reference.
- __launchPrint__: `Function` to trigger the printing on demand.
1. Basic example:

@@ -107,2 +121,3 @@

const d = new Printd()
d.print( document.getElementById('h1'), `h1 { font-family: serif; }` )

@@ -121,19 +136,19 @@ ```

const callback = (win, doc, node, launchPrint) => {
// trigger the printing
launchPrint(win)
}
// trigger the print dialog on demand
const printCallback = ({ launchPrint }) => launchPrint()
d.print(document.getElementById('mycode'), cssText, callback)
d.print(document.getElementById('mycode'), cssText, printCallback)
```
#### getIFrame
### getIFrame
Returns the current `HTMLIFrameElement` reference.
Gets the current `HTMLIFrameElement` reference.
### Events
Example:
```ts
const d = new Printd()
const { contentWindow } = d.getIFrame()
// subscribe to `beforeprint` and `afterprint` events
contentWindow.addEventListener('beforeprint', () => console.log('before print!'))

@@ -143,3 +158,3 @@ contentWindow.addEventListener('afterprint', () => console.log('after print!'))

__Browser compatibility:__
## Browser compatibility

@@ -154,3 +169,4 @@ - Chrome Desktop 63+

References:
__References:__
- [Chrome Platform Status - beforeprint and afterprint events](https://www.chromestatus.com/features/5700595042222080)

@@ -161,3 +177,3 @@ - https://caniuse.com/#feat=beforeafterprint

__Webkit-based and old browsers__
### Webkit-based and old browsers

@@ -180,3 +196,3 @@ For Webkit-based browsers, it can create an equivalent result using `window.matchMedia('print')`.

References:
__References:__
- https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint

@@ -183,0 +199,0 @@ - https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc