vue-pdfiy
v0.0.11
Published
A powerful Vue 3 composable for creating PDF documents with jsPDF and jsPDF-AutoTable. Build PDFs programmatically with a clean, fluent API.
Maintainers
Readme
vue-pdfiy
A powerful Vue 3 composable for creating PDF documents with jsPDF and jsPDF-AutoTable. Build PDFs programmatically with a clean, fluent API.
[!WARNING] ⚠️ Under Active Development
This package is currently under active maintenance and development. While it's functional, the API may change and some features may not be fully stable yet. We recommend:
- Use with caution in production environments
- Pin to a specific version in your
package.json- Report any issues you encounter on GitHub Issues
We're working hard to stabilize the package and appreciate your patience and feedback!
Features
- 📄 Simple PDF Creation - Create PDFs with ease using Vue 3 composables
- 📊 Table Builder - Fluent API for dynamic table generation
- 🎨 Rich Styling - Comprehensive styling options for tables and content
- 🔧 TypeScript Support - Full TypeScript definitions included
- 🚀 Performant - Built on top of jsPDF and jsPDF-AutoTable
- 💪 Flexible - Both declarative and programmatic APIs available
Installation
npm install vue-pdfiy
# or
yarn add vue-pdfiy
# or
bun add vue-pdfiyQuick Start
Basic PDF Creation
import { useJsPdf } from 'vue-pdfiy'
const { addText, setFontSize, savePdf } = useJsPdf({
orientation: 'p',
unit: 'mm',
format: 'a4'
})
setFontSize(20)
addText('Hello, PDF!', 20, 20)
savePdf('hello.pdf')Simple Table
import { useJsPdf } from 'vue-pdfiy'
const { createTable, savePdf } = useJsPdf({ orientation: 'p', unit: 'mm', format: 'a4' })
createTable({
header: [['Name', 'Age', 'City']],
body: [
['John', 30, 'NYC'],
['Jane', 25, 'LA']
]
}, { theme: 'grid' })
savePdf('table.pdf')TableBuilder API
For dynamic, programmatic table creation, use the TableBuilder:
Basic Example
import { useJsPdf } from 'vue-pdfiy'
const { createTableBuilder, savePdf } = useJsPdf({ orientation: 'p', unit: 'mm', format: 'a4' })
createTableBuilder()
.addHeader(['Product', 'Price', 'Stock'])
.addRow(['Laptop', '$999', 50])
.addRow(['Mouse', '$29', 200])
.setTheme('striped')
.setHeaderStyles({ fillColor: '#4CAF50', textColor: '#fff' })
.build()
savePdf('products.pdf')Dynamic Generation
const builder = createTableBuilder()
.addHeader(['Name', 'Score'])
for (const student of students) {
builder.addRow([student.name, student.score])
}
builder
.setTheme('grid')
.setAlternateRowStyles({ fillColor: '#f5f5f5' })
.build()With Helper Utilities
import { useJsPdf, fromObjects, formatCurrency } from 'vue-pdfiy'
const { createTableBuilder, savePdf } = useJsPdf({ orientation: 'p', unit: 'mm', format: 'a4' })
const data = [
{ product: 'Laptop', price: 999, quantity: 5 },
{ product: 'Mouse', price: 29, quantity: 10 }
]
const { header, body } = fromObjects(data)
createTableBuilder()
.setHeader(header)
.addRows(body)
.build()
savePdf('report.pdf')Documentation
- TableBuilder API Documentation - Comprehensive API reference
- Examples - Real-world usage examples
API Overview
useJsPdf Composable
The main composable provides methods for PDF creation:
PDF Methods
pdf- Access to the underlying jsPDF instancefileId(id)- Set PDF file IDsavePdf(filename)- Save PDF to file
Text Methods
addText(text, x, y)- Add text to PDFsetFont(name, style, weight)- Set fontsetFontSize(size)- Set font sizetextColor(r, g, b, alpha)- Set text colorloadCustomFontFn(path, name, family, style)- Load custom font
Page Methods
addPage(format, orientation)- Add new pagesetPage(pageNumber)- Set active pagemovePage(target, before)- Move pagedeletePage(pageNumber)- Delete page
Drawing Methods
drawLine(x1, y1, x2, y2, style)- Draw linecreateCircle(x, y, r, style)- Draw circlelineCap(cap)- Set line cap stylelineDashPattern(pattern, phase)- Set dash patternlineWidth(width)- Set line widthdrawColor(r, g, b, alpha)- Set draw colorfillColor(r, g, b, alpha)- Set fill color
Table Methods
createTable(content, options)- Create table (declarative)createTableBuilder(config)- Create table builder (programmatic)
Helper Utilities
fromObjects(data)- Convert object array to table datafromKeys(obj)- Extract keys from objectformatCurrency(value, currency)- Format number as currencyformatDate(date, format)- Format datecreateTotalRow(label, data, columnIndices)- Create total row
TableBuilder Methods
Header
addHeader(row)- Add header rowsetHeader(rows)- Set entire headerclearHeader()- Clear header
Body
addRow(row)- Add single rowaddRows(rows)- Add multiple rowssetBody(rows)- Set entire bodyclearBody()- Clear body
Footer
addFooter(row)- Add footer rowsetFooter(rows)- Set entire footerclearFooter()- Clear footer
Styling
setTheme(theme)- Set table themesetHeaderStyles(styles)- Style headersetBodyStyles(styles)- Style bodysetFooterStyles(styles)- Style footersetAlternateRowStyles(styles)- Style alternate rowssetColumnStyles(index, styles)- Style specific column
Layout
setMargin(margin)- Set table marginssetStartY(y)- Set starting Y positionsetTableWidth(width)- Set table width
Build
build(pdf?)- Build and render tablegetContent()- Get content configurationgetOptions()- Get options configurationreset()- Reset builderclone()- Clone builder
When to Use What
Use createTable when:
- ✓ You have all data upfront
- ✓ Simple table structure
- ✓ One-time generation
Use createTableBuilder when:
- ✓ Building tables dynamically
- ✓ Conditional rows/styling
- ✓ Programmatic generation in loops
- ✓ Complex styling requirements
TypeScript Support
Full TypeScript definitions are included:
import type {
TableBuilder,
TableBuilderConfig,
TableHelpers,
AutoTableContent,
AutoTableOptions
} from 'vue-pdfiy'Examples
Check out the examples directory for comprehensive usage examples:
- Simple tables
- Styled tables
- Dynamic generation
- Multi-page reports
- Invoice generation
- And more...
Recommended IDE Setup
VS Code + Vue (Official) (and disable Vetur).
Recommended Browser Setup
- Chromium-based browsers (Chrome, Edge, Brave, etc.):
- Firefox:
Type Support for .vue Imports in TS
TypeScript cannot handle type information for .vue imports by default, so we replace the tsc CLI with vue-tsc for type checking. In editors, we need Volar to make the TypeScript language service aware of .vue types.
Customize configuration
See Vite Configuration Reference.
Project Setup
bun installCompile and Hot-Reload for Development
bun devType-Check, Compile and Minify for Production
bun run buildRun Unit Tests with Vitest
bun test:unitLint with ESLint
bun lint