html-export-kit
v0.1.0
Published
Export HTML to PDF and DOCX documents for modern web applications
Maintainers
Readme
html-export-kit
Export HTML to PDF and DOCX documents for modern web applications.
Features
- Convert HTML to PDF and DOCX formats
- Works with Next.js and other React frameworks
- Client-side only to avoid SSR issues
- TypeScript support with full type definitions
- Customizable export options
Installation
npm install html-export-kit
# or
yarn add html-export-kitUsage
Export HTML to PDF
import { toPdf } from 'html-export-kit';
// In a client-side component or event handler
const handleExportPdf = async () => {
const htmlContent = '<h1>Hello World</h1><p>This is a test document.</p>';
await toPdf(htmlContent, {
filename: 'document.pdf',
margin: 10,
jsPDF: {
format: 'a4',
orientation: 'portrait'
}
});
};Export HTML to DOCX (Word)
import { toDocx } from 'html-export-kit';
// In a client-side component or event handler
const handleExportDocx = async () => {
const htmlContent = '<h1>Hello World</h1><p>This is a test document.</p>';
await toDocx(htmlContent, {
filename: 'document.docx',
title: 'Test Document',
download: true
});
};Use with React Elements or DOM Elements
import { toPdf, toDocx } from 'html-export-kit';
// In a client-side component
const handleExport = async () => {
// Get the DOM element
const element = document.getElementById('content-to-export');
// Export to PDF
await toPdf(element);
// Or export to DOCX
await toDocx(element);
};API Reference
toPdf(html, options)
Converts HTML to PDF format.
Parameters:
html(string | HTMLElement): The HTML content or element to convertoptions(object, optional): Configuration optionsfilename(string): Name of the output file (default: 'document-[timestamp].pdf')margin(number | object): Page margins in mmimage(object): Image optionshtml2canvas(object): Options for html2canvasjsPDF(object): Options for jsPDFsave(boolean): Whether to save the file automatically (default: true)
Returns: Promise<Blob>
toDocx(html, options)
Converts HTML to DOCX format.
Parameters:
html(string | HTMLElement): The HTML content or element to convertoptions(object, optional): Configuration optionsfilename(string): Name of the output file (default: 'document-[timestamp].docx')title(string): Document titlemargin(object): Page margins in twip (1/20 of a point)pageSize(string): Page size (default: 'Letter')download(boolean): Whether to download the file automatically (default: false)
Returns: Promise<Blob>
License
MIT
