electron-print-document
v0.1.1
Published
Printing library for Electron.
Maintainers
Readme
electron-print-document
Printing library for Electron.
Install
npm install electron-print-documentBasic Usage
import { printDocument } from "electron-print-document";
await printDocument({
document: {
title: "Receipt",
html: `
<section style="padding: 8mm; font-family: sans-serif;">
<h1 style="margin: 0 0 4mm;">My Store</h1>
<p style="margin: 0 0 2mm;">Order #1024</p>
<p style="margin: 0;">Thank you.</p>
</section>
`,
},
page: {
size: "receipt-80mm",
margins: 0,
},
print: {
silent: true,
printerName: "XP-80C",
copies: 1,
},
});Option Layout
document: HTML, title, styles, and document metadata.page: paper size, margins, and orientation.print: printer behavior such assilent,printerName, andcopies.window/loadUrl: Electron window and loading options for advanced cases.
SSR Usage
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { printDocument } from "electron-print-document";
function Receipt({ order }) {
return (
<section className="receipt">
<h1>Northwind Coffee</h1>
<p>Order {order.number}</p>
<p>{order.paidAt}</p>
<div className="receipt-items">
{order.items.map((item) => (
<div key={item.name} className="receipt-item">
<span>{item.name}</span>
<strong>{item.total}</strong>
</div>
))}
</div>
<div className="receipt-total">
<span>Total</span>
<strong>{order.total}</strong>
</div>
</section>
);
}
await printDocument({
document: {
title: order.number,
styles: `
body {
font-family: "SF Pro Text", "Segoe UI", sans-serif;
color: #111827;
}
.receipt {
padding: 5mm 4.5mm 6mm;
font-size: 12px;
line-height: 1.35;
background: #fff;
}
.receipt-items {
margin: 4mm 0;
}
.receipt-item,
.receipt-total {
display: flex;
justify-content: space-between;
}
`,
html: () => renderToStaticMarkup(<Receipt order={order} />),
},
page: {
size: "receipt-80mm",
margins: 0,
},
});Page Size
Built-in page sizes:
A0A1A2A3A4A5A6LegalLetterTabloidreceipt-44mmreceipt-57mmreceipt-58mmreceipt-76mmreceipt-78mmreceipt-80mm
Label sizes:
label-40x30mmlabel-50x30mmlabel-60x40mm
Use the printer default page size:
page: {
size: "printer-default",
}Custom size:
page: {
size: {
width: 80,
height: 200,
unit: "mm",
},
}Orientation belongs to page:
page: {
size: "A4",
orientation: "landscape",
}print does not accept page size, margin, or orientation options. Keep paper layout in page, and keep printer behavior in print.
If height is omitted, the size is treated as a roll / long paper layout.
Content Overflow
When content exceeds the page area, the default behavior is to log a warning and continue. You can change this with the fit option:
await printDocument({
document: { html: "<div>...</div>" },
page: { size: "label-60x40mm" },
fit: {
overflow: "throw", // "warn" (default) | "throw" | "ignore"
},
});Use "throw" for label printing where every millimeter matters, or "ignore" to suppress the warning entirely.
Window & Print Behavior
The hidden print window is automatically closed when printing finishes.
For previewDocument, pass openDevTools to inspect the rendered document:
const { window } = await previewDocument({
document: { html },
page: { size: "receipt-80mm" },
openDevTools: true,
});Main Exports
printDocumentpreviewDocumentbuildPrintDocumentresolvePageSizePAPER_PRESETS
