Skip to content

v-print

Print specific elements with customizable options. Perfect for generating printable documents.

Since: 1.3.0

Usage

Basic

vue
<template>
  <div v-print>
    <h1>Invoice</h1>
    <p>Click anywhere on this element to print.</p>
  </div>
</template>

With Options

vue
<template>
  <div v-print="{ title: 'My Document', preview: true }">
    <h1>Report</h1>
    <p>Content to print...</p>
  </div>
  <button @click="printDocument">Print</button>
</template>

API

Types

typescript
interface PrintOptions {
  title?: string
  preview?: boolean
  timeout?: number // default: 1000
  beforePrint?: () => void | Promise<void>
  afterPrint?: () => void
  styles?: string
  media?: string // default: 'print'
}

Options

OptionTypeDefaultDescription
titlestringDocument titlePrint document title
previewbooleanfalseShow print preview
timeoutnumber1000Timeout for print dialog
beforePrint() => void | Promise<void>-Callback before printing
afterPrint() => void-Callback after printing
stylesstring-Additional CSS styles for print
mediastring'print'Media type for styles

Examples

Invoice Printing

vue
<template>
  <div v-print="{ title: 'Invoice #' + invoiceId, beforePrint: formatInvoice }">
    <h1>Invoice #{{ invoiceId }}</h1>
    <table>
      <tr v-for="item in items" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.price }}</td>
      </tr>
    </table>
    <div class="total">Total: {{ total }}</div>
  </div>
</template>

Custom Print Styles

vue
<template>
  <article v-print="{
    title: 'Article',
    styles: '@page { margin: 2cm; } body { font-size: 12pt; }'
  }">
    <h1>{{ title }}</h1>
    <p>{{ content }}</p>
  </article>
</template>

Composable API

For programmatic use, you can use the usePrint composable:

typescript
import { usePrint, quickPrint } from 'directix'

const { isPrinting, print, printPage } = usePrint({
  title: 'My Document',
  styles: '@page { margin: 2cm; }',
  cssUrls: ['/print.css'],
  onBeforePrint: () => console.log('About to print...'),
  onAfterPrint: () => console.log('Print complete!'),
  newWindow: false,
  printClass: 'print-content'
})

// Print specific element
await print('#content')

// Print current page
await printPage()

// Quick print function
quickPrint('#content', { title: 'Quick Print' })

UsePrintOptions

OptionTypeDefaultDescription
titlestring | Ref<string>-Title for printed document
stylesstring | string[] | Ref-Additional CSS styles
cssUrlsstring[] | Ref<string[]>[]Additional CSS URLs
onBeforePrint() => boolean | void-Callback before printing (return false to cancel)
onAfterPrint() => void-Callback after printing
newWindowboolean | Ref<boolean>falsePrint in new window
printClassstring | Ref<string>-Custom class for print container

UsePrintReturn

PropertyTypeDescription
isPrintingRef<boolean>Whether printing is in progress
print(target?: string | HTMLElement) => Promise<void>Print specified element
printPage() => Promise<void>Print current page

Example

vue
<script setup>
import { usePrint } from 'directix'

const { isPrinting, print } = usePrint({
  title: 'My Document',
  onAfterPrint: () => console.log('Print complete!')
})

async function handlePrint() {
  await print('#content')
}
</script>

<template>
  <div>
    <button @click="handlePrint" :disabled="isPrinting">
      {{ isPrinting ? 'Printing...' : 'Print' }}
    </button>
    <div id="content">Content to print</div>
  </div>
</template>

Code Generator

Quick Code Generator
<template>
  <div v-print>
    <!-- Print specific elements with customizable options. Perfect for generating printable documents. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = {}
</script>

Released under the MIT License.