printing
Help to print specified DOMs instead of whole page.
Installation
npm install printing --save
yarn add printing
Usage
import print from 'printing'
const target document.getElementById('target')
const options = {
beforePrint (window) {},
direction: 'vertical',
scanStyles: true,
css: '',
injectGlobalCss: false,
}
print.preview(target, options)
print(target, options).then(() => {
})
API
interface Options {
beforePrint?: (window: Window) => void
direction?: 'vertical' | 'horizontal'
scanStyles?: boolean | string[] | 'common'
css?: string
injectGlobalCss?: boolean
}
function print (source: HTMLElement | HTMLElement[], options: Options = {}): Promise<void>
function preview (source: HTMLElement | HTMLElement[], options: Options = {}): Promise<void>
- DOM attributes:
data-print-style
: add styles to DOM when printingdata-print-class
: add class names to DOM when printing
<div
style="border: 1px solid blue;"
data-print-style="border-color: red;"
data-print-class="my-div"
>
...
</div>
Notes
- All style with percentage (such as
width: 100%
) will be invalid while printing (due to getComputedStyle
), and you can use data-print-style
to fix it.