All Projects → joseluisq → Printd

joseluisq / Printd

Licence: mit
Print HTML elements or pages in modern browsers.

Programming Languages

typescript
32286 projects

Labels

Projects that are alternatives of or similar to Printd

printer
A fancy logger yet lightweight, and configurable. 🖨
Stars: ✭ 65 (+35.42%)
Mutual labels:  print
Beeprint
make your debug printing more friendly
Stars: ✭ 348 (+625%)
Mutual labels:  print
Wp Print
Displays a printable version of your WordPress blog's post/page.
Stars: ✭ 16 (-66.67%)
Mutual labels:  print
objprint
A library that can print Python objects in human readable format
Stars: ✭ 141 (+193.75%)
Mutual labels:  print
Redux Offline Docs
Redux documentation in PDF, ePub and MOBI formats for offline reading.
Stars: ✭ 292 (+508.33%)
Mutual labels:  print
Hartija Css Print Framework
Universal CSS for web printing
Stars: ✭ 509 (+960.42%)
Mutual labels:  print
senaite.impress
HTML to PDF Rendering Engine for SENAITE
Stars: ✭ 16 (-66.67%)
Mutual labels:  print
Bashmultitool
A library for bash shell program containing useful functions. Can be imported into scripts to create colourful and functional scripts and TUIs.
Stars: ✭ 27 (-43.75%)
Mutual labels:  print
Prettyprinter
Syntax-highlighting, declarative and composable pretty printer for Python 3.5+
Stars: ✭ 304 (+533.33%)
Mutual labels:  print
Jquery.print
Easy to use, Element Printing Plugin for jQuery
Stars: ✭ 772 (+1508.33%)
Mutual labels:  print
bonaparticle
The LaTeX magazine class that doesn’t get in your way.
Stars: ✭ 20 (-58.33%)
Mutual labels:  print
Cordova Plugin Printer
Print HTML documents
Stars: ✭ 265 (+452.08%)
Mutual labels:  print
Traceback with variables
Adds variables to python traceback. Simple, lightweight, controllable. Debug reasons of exceptions by logging or pretty printing colorful variable contexts for each frame in a stacktrace, showing every value. Dump locals environments after errors to console, files, and loggers. Works in Jupyter and IPython. Install with pip or conda.
Stars: ✭ 509 (+960.42%)
Mutual labels:  print
gscloudplugin
浏览器打印PDF。 浏览器打印HTML。 浏览器打印图片。 浏览器打印Word。浏览器打印Excel。浏览器打印PPT。浏览器打印自定义绘图。浏览器打印微软报表。 使用静默方式打印。蓝牙打印。读写串口数据。读取电子秤重量
Stars: ✭ 18 (-62.5%)
Mutual labels:  print
Printthis
jQuery printing plugin; print specific elements on a page
Stars: ✭ 902 (+1779.17%)
Mutual labels:  print
react-native-star-prnt
React-Native bridge to communicate with Star Micronics Bluetooth/LAN Printers
Stars: ✭ 61 (+27.08%)
Mutual labels:  print
Gutenberg
Modern framework to print the web correctly.
Stars: ✭ 4,425 (+9118.75%)
Mutual labels:  print
Logcustom
A simple log customization tool based on golang 一个基于golang简单的日志定制化工具
Stars: ✭ 46 (-4.17%)
Mutual labels:  print
Mapbox Gl Print Export For Port
Print/Export for Mapbox GL
Stars: ✭ 14 (-70.83%)
Mutual labels:  print
Pocorgtfo
a "Proof of Concept or GTFO" mirror with extra article index, direct links and clean PDFs.
Stars: ✭ 560 (+1066.67%)
Mutual labels:  print

Printd CI npm npm JavaScript Style Guide

Print HTML elements or pages in modern browsers. 🖨

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()
})

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:

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 José Quintana

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].