From 51d0decc73c91ef81bde2d48349fecc2d8bc4016 Mon Sep 17 00:00:00 2001 From: Novout Date: Sat, 17 Jun 2023 11:00:26 -0300 Subject: [PATCH] feat(factory): text relative positions --- demo/src/main.ts | 5 +++-- packages/pdfeasy/src/font/vfs.ts | 4 ++-- packages/pdfeasy/src/pipe/emitter.ts | 4 ++-- packages/pdfeasy/src/pipe/factory.ts | 12 +++++++++++- packages/pdfeasy/src/pipe/setter.ts | 4 ++-- packages/pdfeasy/src/plugins/background.ts | 4 ++-- packages/pdfeasy/src/plugins/page.ts | 6 +++--- packages/pdfeasy/src/runner/pdfeasy.ts | 13 +++++++++++-- packages/pdfeasy/src/types.ts | 17 +++-------------- packages/pdfeasy/src/utils/defines.ts | 1 + 10 files changed, 40 insertions(+), 30 deletions(-) diff --git a/demo/src/main.ts b/demo/src/main.ts index 093de2a..7d8d945 100644 --- a/demo/src/main.ts +++ b/demo/src/main.ts @@ -19,9 +19,10 @@ pdfeasy.new({ }) pdfeasy.add([ - { raw: 'A simple pdf', text: {} }, + { raw: 'A simple pdf', text: {}}, { lineBreak: {} }, - { raw: 'using...', text: {} }, + { raw: 'using...', text: { position: { x: 250, y: 0 }} }, + { raw: 'hm...', text: {} }, { pageBreak: {} }, { raw: 'pdfeasy!', text: {} }, ...Utils.content(), diff --git a/packages/pdfeasy/src/font/vfs.ts b/packages/pdfeasy/src/font/vfs.ts index 4840b19..516fb8b 100644 --- a/packages/pdfeasy/src/font/vfs.ts +++ b/packages/pdfeasy/src/font/vfs.ts @@ -1,4 +1,4 @@ -import pdfeasy from '../runner/pdfeasy' +import { PDFEasy } from '../runner/pdfeasy' import path from 'path' import { getBase64ByURL } from '../utils/request' import { regex } from '../utils/defines' @@ -25,7 +25,7 @@ export const setServerPath = (p: string) => { return path.join(process.cwd() + `/${p}`) } -export const setExternalFonts = async (instance: pdfeasy) => { +export const setExternalFonts = async (instance: PDFEasy) => { if (instance.options?.advanced?.fontsPurge) { const allContentFonts: string[] = [] diff --git a/packages/pdfeasy/src/pipe/emitter.ts b/packages/pdfeasy/src/pipe/emitter.ts index 7c8a1fa..3546097 100644 --- a/packages/pdfeasy/src/pipe/emitter.ts +++ b/packages/pdfeasy/src/pipe/emitter.ts @@ -1,6 +1,6 @@ -import pdfeasy from '../runner/pdfeasy' +import { PDFEasy } from '../runner/pdfeasy' -export const onPageAdded = (instance: pdfeasy, cb: any) => { +export const onPageAdded = (instance: PDFEasy, cb: any) => { instance.pdfkit?.on('pageAdded', () => { cb && cb() }) diff --git a/packages/pdfeasy/src/pipe/factory.ts b/packages/pdfeasy/src/pipe/factory.ts index fe1e765..26558a8 100644 --- a/packages/pdfeasy/src/pipe/factory.ts +++ b/packages/pdfeasy/src/pipe/factory.ts @@ -72,12 +72,22 @@ export const resolveContent = async ( const data = embed ? ` ${raw || content.raw}` : raw || content.raw + const possibleLastPos = globals.__LAST_CONTENT__?.text?.position + + let pos = style?.position + ? { x: app.x + style.position.x, y: app.y + style.position.y } + : { x: app.x, y: app.y } + if (possibleLastPos) { + pos.x -= possibleLastPos.x + pos.y -= possibleLastPos.y + } + await app .font(getCorrectFontFamily(style?.font || defaults.text.font, style)) .fontSize(style?.fontSize || defaults.text.fontSize) .fillColor(resolveColor(style?.color || defaults.text.color, run)) .fillOpacity(style?.opacity || defaults.text.opacity) - .text(data, { + .text(data, pos.x, pos.y, { indent: style?.indent || defaults.text.indent, align: style?.align || defaults.text.align, paragraphGap: style?.paragraphMargin || defaults.text.paragraphMargin, diff --git a/packages/pdfeasy/src/pipe/setter.ts b/packages/pdfeasy/src/pipe/setter.ts index 736522d..a8ef139 100644 --- a/packages/pdfeasy/src/pipe/setter.ts +++ b/packages/pdfeasy/src/pipe/setter.ts @@ -1,8 +1,8 @@ import { getImageRaw } from '../content/image' import { regex } from '../utils/defines' -import pdfeasy from '../runner/pdfeasy' +import { PDFEasy } from '../runner/pdfeasy' -export const setBackground = async (instance: pdfeasy, str: string) => { +export const setBackground = async (instance: PDFEasy, str: string) => { if (!instance.pdfkit) return const kit = instance.pdfkit diff --git a/packages/pdfeasy/src/plugins/background.ts b/packages/pdfeasy/src/plugins/background.ts index e82c203..607f7e2 100644 --- a/packages/pdfeasy/src/plugins/background.ts +++ b/packages/pdfeasy/src/plugins/background.ts @@ -1,7 +1,7 @@ import { setBackground } from '../pipe/setter' -import pdfeasy from '../runner/pdfeasy' +import { PDFEasy } from '../runner/pdfeasy' -export const runPluginBackground = async (instance: pdfeasy) => { +export const runPluginBackground = async (instance: PDFEasy) => { if (instance.options?.plugins) { for (const plugin of instance.options.plugins) { if (plugin.background && instance.globals.__NEW_PAGE__) { diff --git a/packages/pdfeasy/src/plugins/page.ts b/packages/pdfeasy/src/plugins/page.ts index 8825256..4d6782a 100644 --- a/packages/pdfeasy/src/plugins/page.ts +++ b/packages/pdfeasy/src/plugins/page.ts @@ -1,4 +1,4 @@ -import pdfeasy from '../runner/pdfeasy' +import { PDFEasy } from '../runner/pdfeasy' import { getCorrectFontFamily } from '../pipe/transform' import { getImageRaw } from '../content/image' import { @@ -9,7 +9,7 @@ import { PluginPageTextOptions, } from 'src/types' -export const generate = (instance: pdfeasy): PluginGenerate => { +export const generate = (instance: PDFEasy): PluginGenerate => { const kit = instance.pdfkit as PDFKit.PDFDocument const defaults = instance.def @@ -101,7 +101,7 @@ export const generate = (instance: pdfeasy): PluginGenerate => { return { Text, Image } } -export const pageHandler = (instance: pdfeasy): Promise => { +export const pageHandler = (instance: PDFEasy): Promise => { return new Promise(async (response, reject) => { const doc = instance.pdfkit diff --git a/packages/pdfeasy/src/runner/pdfeasy.ts b/packages/pdfeasy/src/runner/pdfeasy.ts index 22c5f6f..7ab55f0 100644 --- a/packages/pdfeasy/src/runner/pdfeasy.ts +++ b/packages/pdfeasy/src/runner/pdfeasy.ts @@ -99,6 +99,7 @@ export class PDFEasy { __BACKGROUND_RAW__: '', }, __LAST_TYPE__: ['paragraph', 0], + __LAST_CONTENT__: {}, } private mutateLastType = (type: ItemType) => { @@ -109,6 +110,10 @@ export class PDFEasy { : [type, 1] } + private posUpdateContent = (content: Content) => { + this.globals.__LAST_CONTENT__ = content + } + private getType = (content: Content): ItemType => { if (content.checkbox) return 'checkbox' if (content.list) return 'list' @@ -137,10 +142,10 @@ export class PDFEasy { for (const content of this.contents) { await runPluginBackground(this) - this.mutateLastType(this.getType(content)) - if (!this.pdfkit) return + this.mutateLastType(this.getType(content)) + await resolveContent( this.pdfkit, this.def, @@ -148,6 +153,8 @@ export class PDFEasy { this.globals, this.optionsRun ) + + this.posUpdateContent(content) } } @@ -171,6 +178,7 @@ export class PDFEasy { __BACKGROUND_RAW__: '', }, __LAST_TYPE__: ['paragraph', 1], + __LAST_CONTENT__: {}, } } @@ -290,6 +298,7 @@ export class PDFEasy { */ public run = (options?: RunOptions): Promise => { this.optionsRun = options || {} + this.globals.__LAST_CONTENT__ = this.contents[0] const runType = options?.type || 'client' diff --git a/packages/pdfeasy/src/types.ts b/packages/pdfeasy/src/types.ts index c55a8e6..7763269 100644 --- a/packages/pdfeasy/src/types.ts +++ b/packages/pdfeasy/src/types.ts @@ -166,6 +166,7 @@ export interface InternalGlobals { __BACKGROUND_RAW__: string, }, __LAST_TYPE__: [ItemType, number] + __LAST_CONTENT__: Record } export interface ImageRaw { @@ -181,20 +182,7 @@ export interface ExternalFont { bolditalic: string } -export interface ContentText { - fontSize?: number - font?: Fonts - color?: string - indent?: number - align?: TextAlign - paragraphMargin?: number - lineHeight?: number - opacity?: number - destination?: string - go?: string - bold?: boolean - italic?: boolean -} +export interface ContentText extends Partial {} export interface DefaultsText { fontSize: number @@ -209,6 +197,7 @@ export interface DefaultsText { go: string | undefined bold: boolean italic: boolean + position: { x: number, y: number } } export interface ContentImage { diff --git a/packages/pdfeasy/src/utils/defines.ts b/packages/pdfeasy/src/utils/defines.ts index ffcb905..78cdbe3 100644 --- a/packages/pdfeasy/src/utils/defines.ts +++ b/packages/pdfeasy/src/utils/defines.ts @@ -15,6 +15,7 @@ export const pdfDefaults = (): PDFEasyDefaults => { go: undefined, bold: false, italic: false, + position: { x: 0, y: 0 }, }, lineBreak: { spacing: 5,