Skip to content

Commit

Permalink
refactor: Improve function exports (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoey-kaiser authored Mar 4, 2024
1 parent fbddec7 commit 0ceee35
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 25 deletions.
6 changes: 3 additions & 3 deletions playground/server/api/pdf/withLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPDF, streamReturnPDF } from '#pdf'
import { createPDF, streamReturnPDF, drawHorizontalLine, applyLayout } from '#pdf'

export default eventHandler(async (event) => {
const pdf = createPDF({info: { Title: 'Welcome to NuxtPDF!' }}, undefined, {
Expand All @@ -9,7 +9,7 @@ export default eventHandler(async (event) => {
await new Promise(resolve => setTimeout(resolve, 100))
doc.moveDown(1.5)
doc.text('Welcome to NuxtPDF!')
doc.horizontalLine(0.5)
drawHorizontalLine(doc, 0.5)
}
},
footer: {
Expand All @@ -24,7 +24,7 @@ export default eventHandler(async (event) => {
pdf.addPage()
pdf.text('Its pretty nice.')

await pdf.applyLayout()
await applyLayout(pdf)
pdf.end()

return streamReturnPDF(event, pdf)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { PDFDocumentType } from "../../types"

export type HorizontalLine = (moveDown?: number) => void
export function drawHorizontalLine<T>(doc: PDFDocumentType<T>, moveDown = 1) {
/**
* Draw a Horizontal line across the document
* @param doc The PDF document object
* @param moveDown The amount of lines to move down before and after drawing the line. Default: 1
*/
export function drawHorizontalLine<T>(doc: PDFDocumentType<T>, moveDown = 1): void {
doc
.moveDown(moveDown)
.moveTo(doc.options.margins.left, doc.y)
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/server/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { drawHorizontalLine } from './drawHorizontalLine'
export { applyLayout } from './layout'
7 changes: 5 additions & 2 deletions src/runtime/server/components/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ async function printHeaders<T>(doc: PDFDocumentType<T>) {
}
}

export type ApplyLayout = () => Promise<void>
export async function applyLayout<T>(doc: PDFDocumentType<T>) {
/**
* Applies the Header and Footer designs to every page. Requires layout option to be set on PDF initialization
* @param doc The PDF document object
*/
export async function applyLayout<T>(doc: PDFDocumentType<T>): Promise<void> {
await printHeaders(doc)
await printFooters(doc)
}
2 changes: 2 additions & 0 deletions src/runtime/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { createPDF, streamReturnPDF } from './pdf'
export * from './components'

export type { PDFDocumentType, PDFOptions } from '../types'
13 changes: 4 additions & 9 deletions src/runtime/server/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import type { H3Event } from 'h3'
import type { WriteStream } from 'node:fs'
import type { PDFOptions, PDFDocumentType } from '../types'

import { drawHorizontalLine } from './components/line'
import { applyLayout } from './components/layout'

export interface LayoutOptions {
header?: {
render: <T>(doc: PDFDocumentType<T>) => Promise<void> | void
Expand Down Expand Up @@ -40,13 +37,11 @@ export function createPDF<TData>(options?: PDFKit.PDFDocumentOptions, data?: TDa

// Init PDF
const doc = new PDFDocument(formattedOptions) as PDFDocumentType<TData>
if (streamToFile) { doc.pipe(streamToFile) }
if (data) { doc.data = data }
if (layout) { doc.layout = layout }

// Inject Component functions
doc.horizontalLine = (moveDown: number = 1) => drawHorizontalLine(doc, moveDown)
doc.applyLayout = () => applyLayout(doc)
// Inject futhur PDF data
doc.data = data
doc.layout = layout
if (streamToFile) { doc.pipe(streamToFile) }

return doc
}
Expand Down
9 changes: 0 additions & 9 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import PDFDocument from 'pdfkit'
import type { HorizontalLine } from './server/components/line'
import type { ApplyLayout } from './server/components/layout'
import type { LayoutOptions } from './server/pdf'

export interface PDFOptions extends PDFKit.PDFDocumentOptions {
Expand All @@ -22,11 +20,4 @@ export interface ModuleOptions {
export type PDFDocumentType<TData> = PDFDocument & {
data?: TData
layout?: LayoutOptions

/**
* Draw a Horizontal line across the document
* @param moveDown The amount of lines to move down before and after drawing the line. Default: 1
*/
horizontalLine: HorizontalLine
applyLayout: ApplyLayout
}

0 comments on commit 0ceee35

Please sign in to comment.