diff --git a/playground/server/api/pdf/withLayout.ts b/playground/server/api/pdf/withLayout.ts index 5e68a01..e84f755 100644 --- a/playground/server/api/pdf/withLayout.ts +++ b/playground/server/api/pdf/withLayout.ts @@ -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, { @@ -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: { @@ -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) diff --git a/src/runtime/server/components/line.ts b/src/runtime/server/components/drawHorizontalLine.ts similarity index 58% rename from src/runtime/server/components/line.ts rename to src/runtime/server/components/drawHorizontalLine.ts index 08f9b83..c0f2a2a 100644 --- a/src/runtime/server/components/line.ts +++ b/src/runtime/server/components/drawHorizontalLine.ts @@ -1,7 +1,11 @@ import type { PDFDocumentType } from "../../types" -export type HorizontalLine = (moveDown?: number) => void -export function drawHorizontalLine(doc: PDFDocumentType, 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(doc: PDFDocumentType, moveDown = 1): void { doc .moveDown(moveDown) .moveTo(doc.options.margins.left, doc.y) diff --git a/src/runtime/server/components/index.ts b/src/runtime/server/components/index.ts new file mode 100644 index 0000000..29e51a6 --- /dev/null +++ b/src/runtime/server/components/index.ts @@ -0,0 +1,2 @@ +export { drawHorizontalLine } from './drawHorizontalLine' +export { applyLayout } from './layout' diff --git a/src/runtime/server/components/layout.ts b/src/runtime/server/components/layout.ts index 990e29a..0732ea3 100644 --- a/src/runtime/server/components/layout.ts +++ b/src/runtime/server/components/layout.ts @@ -23,8 +23,11 @@ async function printHeaders(doc: PDFDocumentType) { } } -export type ApplyLayout = () => Promise -export async function applyLayout(doc: PDFDocumentType) { +/** + * 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(doc: PDFDocumentType): Promise { await printHeaders(doc) await printFooters(doc) } diff --git a/src/runtime/server/index.ts b/src/runtime/server/index.ts index cbd611f..0f6ee8b 100644 --- a/src/runtime/server/index.ts +++ b/src/runtime/server/index.ts @@ -1,2 +1,4 @@ export { createPDF, streamReturnPDF } from './pdf' +export * from './components' + export type { PDFDocumentType, PDFOptions } from '../types' diff --git a/src/runtime/server/pdf.ts b/src/runtime/server/pdf.ts index e4464c2..58ff7f8 100644 --- a/src/runtime/server/pdf.ts +++ b/src/runtime/server/pdf.ts @@ -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: (doc: PDFDocumentType) => Promise | void @@ -40,13 +37,11 @@ export function createPDF(options?: PDFKit.PDFDocumentOptions, data?: TDa // Init PDF const doc = new PDFDocument(formattedOptions) as PDFDocumentType - 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 } diff --git a/src/runtime/types.ts b/src/runtime/types.ts index b71f62a..faab5c5 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -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 { @@ -22,11 +20,4 @@ export interface ModuleOptions { export type PDFDocumentType = 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 }