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

refactor: Improve function exports #29

Merged
merged 3 commits into from
Mar 4, 2024
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
Next Next commit
refactored export types of components
  • Loading branch information
zoey-kaiser committed Mar 4, 2024
commit 227484ae140bc095e38e666f5241b074b66b86b0
4 changes: 2 additions & 2 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 } 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 Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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 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
1 change: 1 addition & 0 deletions src/runtime/server/components/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { drawHorizontalLine } from './drawHorizontalLine'
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'
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PDFDocumentType } from "../../types"
import type { PDFDocumentType } from "../types"

async function printFooters<T>(doc: PDFDocumentType<T>) {
if (!doc.layout?.footer) return
Expand Down
8 changes: 3 additions & 5 deletions src/runtime/server/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ 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'
import { applyLayout } from './layout'

export interface LayoutOptions {
header?: {
Expand Down Expand Up @@ -42,10 +41,9 @@ export function createPDF<TData>(options?: PDFKit.PDFDocumentOptions, data?: TDa
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)
// Layout options
if (layout) { doc.layout = layout }
doc.applyLayout = () => applyLayout(doc)

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

export interface PDFOptions extends PDFKit.PDFDocumentOptions {
Expand All @@ -22,11 +21,5 @@ 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
}