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 (el, index) {},
direction: 'vertical'
}
print.preview(target, options)
print(target, options).then(() => {
})
API
interface Options {
beforePrint?: (el: HTMLElement, index: number) => void
direction?: 'vertical' | 'horizontal'
}
function print (source: HTMLElement | HTMLElement[], options: Options = {}): Promise<void>
function preview (source: HTMLElement | HTMLElement[], options: Options = {}): 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.
Changelogs