npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

printd

v1.6.0

Published

Print HTML elements in modern browsers.

Downloads

23,270

Readme

Printd CI npm npm JavaScript Style Guide

Print HTML elements or pages in modern browsers. :printer:

Printd opens your Browser Print Dialog to print HTML elements inside a blank document or pages by URL.

Features

  • Written and tested entirely in Typescript.
  • Tiny script (around 1KB gzipped with no dependencies).
  • Print any element without opening a new window.
  • Print only when assets such as images or fonts are ready (loaded).
  • Print pages by URL.
  • Add styles and scripts on demand using text or URL.

Demos

Install

Yarn

yarn add printd

NPM

npm install printd --save

UMD file is also available on unpkg:

<script src="https://unpkg.com/printd/printd.umd.min.js"></script>

You can use the library via window.printd.

Usage

import { Printd } from 'printd'

const cssText = `
  h1 {
    color: black;
    font-family: sans-serif;
  }
`

const d = new Printd()
d.print( document.getElementById('myelement'), [ cssText ] )

API

options

  • parent: Optional parent element where the printable element will be appended. Default window.document.body
  • headElements: Optional custom document head elements array.
  • bodyElements: Optional custom document body elements array.

Example:

// custom base element example
const base = document.createElement('base')
base.setAttribute('href', 'https://your-cdn.dev')

// define options to use
const options = {
  parent: document.getElementById('myparent'),
  headElements: [ base ]
}

const d = new Printd(options)

print

Function to print an HTMLElement.

d.print (element, styles, scripts, callback)

Print parameters:

  • element: Some HTMLElement object to print.
  • styles: Optional styles (array of texts or urls) that will add to iframe (document.head)
  • scripts: Optional scripts (array of texts or urls) that will add to iframe (document.body)
  • callback: Optional callback that will be triggered when content is ready to print.
    • callback arguments:
    • iframe: An HTMLIFrameElement reference. It already contains contentWindow and contentDocument references.
    • element: An HTMLElement copy (cloned node) reference of current element to print.
    • launchPrint: Function to launch the print dialog after assets (images, fonts, etc) was loaded.

1. Basic example

const d = new Printd()

d.print( document.getElementById('h1'), [`h1 { font-family: serif; }`] )

2. Callback example

Callback option is suitable when you plan to print elements or pages with assets (images, fonts, etc) but you need to wait for them. Your callback will be triggered only when your assets are loaded.

const d = new Printd()

// Tip: texts & urls are supported

const styles = [
  'https://your-cdn.dev/style.css',
  `.code { font-family: monospace; }`
]

const scripts = [
  'https://your-cdn.dev/script.js',
  `(() => console.log('Hello from IFrame!'))()`
]

// Get an HTMLElement reference
const el = document.getElementById('mycode-block')
// Trigger the print dialog on demand
const printCallback = ({ launchPrint }) => launchPrint()

d.print(el, styles, scripts, printCallback)

printURL

Function to print an URL.

PrintURL parameters:

  • url: URL to print.
  • callback: Optional callback that will be triggered when content is ready to print.
const d = new Printd()

d.printURL('http://127.0.0.1/', ({ launchPrint }) => {
  console.log('Content loaded!')

  // fire printing!
  launchPrint()
})

Built-in print events

Printd provides convenient print event methods on top of the Browser beforeprint and afterprint events. However note that they only work when printing custom HTML elements via the print() method.

const d = new Printd()

d.onBeforePrint((event) => console.log(event, "called before printing!"))
d.onAfterPrint((event) => console.log(event, "called after printing!"))

d.print(document.getElementById('myelement'))

getIFrame

Gets the current HTMLIFrameElement reference.

Examples:

const d = new Printd()
const iframe = d.getIFrame()

// a) Subscribe to IFrame load event
iframe.addEventListener('load', () => console.log('iframe loaded!'))

// b) Subscribe to Window `beforeprint` or `afterprint` events
const { contentWindow } = iframe
contentWindow.addEventListener('beforeprint', () => console.log('before print!'))
contentWindow.addEventListener('afterprint', () => console.log('after print!'))

Browser compatibility

  • Chrome Desktop 63+
  • Chrome for Android 63+
  • Firefox 6+
  • Edge
  • Internet Explorer 11
  • Opera Desktop 50+
  • Opera for Android 50+

References:

beforeprint & afterprint workaround (Webkit-based and old browsers)

For Webkit-based browsers, it can create an equivalent result using window.matchMedia('print').

if (contentWindow.matchMedia) {
  const mediaQueryList = contentWindow.matchMedia('print')

  mediaQueryList.addListener((mql) => {
    if (mql.matches) {
      console.log('before print!')
    } else {
      console.log('after print!')
    }
  })
}

References:

  • https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeprint
  • https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/

Security

Since Printd uses an underlying iframe it's highly recommended to ensure that only your content will be displayed. As a fallback you could remove the hidden iframe after some printing.

Here some interesting security advices that you want to take at look:

Contributions

Feel free to send some Pull request or issue.

License

MIT license

© 2017-present Jose Quintana