Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initial implementation of nuxt-pdf #1

Merged
merged 8 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added composable to interact with
  • Loading branch information
zoey-kaiser committed Jan 31, 2023
commit 94bb8e11bca947d2015d9008187d0495bd7e907f
31 changes: 28 additions & 3 deletions docs/content/1.getting-started/3.quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,31 @@ description: "How to use nuxt-pdf."
---

# Quick Start
In this quick start guide you can find an overview of how to use `nuxt-pdf`. For more advanced use cases please refer to our [specialized recipes](/nuxt-pdf/recipes).

After [installing the package](/nuxt-pdf/getting-started/installation), you are able to use the exported `exportToPDF` function within your Nuxt app.
## Usage
After [installing the package](/nuxt-pdf/getting-started/installation), we offer two primary ways to interact with our module.

`exportToPDF` takes two parameters. The first is a HTMLElement and the second being a options object.
We recommend making use of vue's ref system to pass a component into the function. You can see an example of this below:
### Via composable

```vue
<template>
<MyComponent ref="pdfExport" />
</template>

<script setup lang="ts">
import { ref } from 'vue'
const { usePDFExport } = usePDFExport()

const pdfExport = ref<HTMLElement | null>(null)
usePDFExport(pdfExport, {
'fileName': 'myCoolExport.pdf',
// any other options
})
</script>
```

### Via injected variable

```vue
<template>
Expand All @@ -26,6 +46,11 @@ $exportToPDF(pdfExport, {
</script>
```

## Parameters

`nuxt-pdf` takes two parameters. The first is a HTMLElement and the second being an options object.
We recommend making use of vue's ref system to pass a component into the function. You can see an example of this below:

The second parameter is an options object. You can find all available options and their purpose on the [official html2pdf docs](https://ekoopmans.github.io/html2pdf.js/#options).

::alert{type="info"}
Expand Down
6 changes: 2 additions & 4 deletions playground/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
</template>

<script setup lang="ts">
import { useNuxtApp } from '#app'
import { ref } from 'vue'
import { Html2PdfOptions } from '../../src/runtime/plugin.client'

const { $exportToPDF } = useNuxtApp()
import { usePDFExport } from '#imports'

const page = ref<HTMLElement | null>(null)
const pdfSection = ref<HTMLElement | null>(null)

const print = async (element: HTMLElement, options: Html2PdfOptions) => {
await $exportToPDF(element, options)
await usePDFExport(element, options)
}
</script>
6 changes: 2 additions & 4 deletions playground/pages/withAdvancedStyles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,17 @@
</template>

<script setup lang="ts">
import { useNuxtApp } from '#app'
import { ref } from 'vue'
import { Html2PdfOptions } from '../../src/runtime/plugin.client'

const { $exportToPDF } = useNuxtApp()
import { usePDFExport } from '#imports'

const pdfSection = ref<HTMLElement | null>(null)
const isExporting = ref(false)

const print = async (element: HTMLElement, options: Html2PdfOptions) => {
pdfSection.value?.classList.add('print')
isExporting.value = true
await $exportToPDF(element, options)
await usePDFExport(element, options)
pdfSection.value?.classList.remove('print')
isExporting.value = false
}
Expand Down
5 changes: 4 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin, createResolver } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, createResolver, addImportsDir } from '@nuxt/kit'

export default defineNuxtModule({
meta: {
Expand All @@ -13,5 +13,8 @@ export default defineNuxtModule({
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
nuxt.options.build.transpile.push(runtimeDir)
addPlugin(resolve(runtimeDir, 'plugin.client'))

const composables = resolve('./runtime/composables')
addImportsDir(composables)
}
})
5 changes: 5 additions & 0 deletions src/runtime/composables/usePDFExport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useNuxtApp } from '#app'
import { Html2PdfOptions } from '../plugin.client'
export const usePDFExport = async (element: HTMLElement, options: Html2PdfOptions) => {
await useNuxtApp().$exportToPDF(element, options)
}