A lightweight, pure JavaScript utility to print HTML elements by ID — with full support for structured layouts including header, content, and footer.
- ✅ Framework-agnostic — works with Vue, React, Angular, etc.
- ✅ No dependencies
- ✅ Fully DOM-based (no innerHTML hacks)
- ✅ Supports header, content, and footer injection
npm install easy-print-js
# or
yarn add easy-print-js
# or
pnpm add easy-print-js
You can use this package in plain HTML/JS or any framework like Vue, React, Angular, etc.
<script setup lang="ts">
import { easyPrint } from 'easy-print-js'
function printSection() {
easyPrint({
contentElementId: 'invoice',
headerElementId: 'print-header',
footerElementId: 'print-footer'
})
}
</script>
<template>
<div>
<!-- Hidden printable content -->
<div style="display: none">
<div id="print-header">
<h1>Company Name</h1>
</div>
<div id="invoice">
<h2>Invoice #123</h2>
<p>Total: $42</p>
</div>
<div id="print-footer">
<small>Thank you for your business!</small>
</div>
</div>
<!-- Visible trigger -->
<button @click="printSection">Print Invoice</button>
</div>
</template>
Option | Type | Required | Description |
---|---|---|---|
contentElementId |
string |
✅ | The ID of the main body content element |
headerElementId |
string |
❌ | Optional element to show at the top of print |
footerElementId |
string |
❌ | Optional element to show at the bottom |
The elements are cloned and inserted into a print-specific structure using
<thead>
,<tbody>
, and<tfoot>
.
MIT © Ali Hassan
GitHub: https://github.com/alihdev/easy-print-js
⭐ If you find this project useful, give it a star on GitHub — it helps others discover it!
Contributions are welcome! If you find bugs, have suggestions, or want to add a new feature, feel free to:
- Fork the repository
- Create a new branch (
git checkout -b feature/my-feature
) - Commit your changes (
git commit -am 'Add new feature'
) - Push to the branch (
git push origin feature/my-feature
) - Open a pull request
All contributions are reviewed and appreciated 🌟