Skip to content

Commit

Permalink
feat(factory): text relative positions
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Jun 17, 2023
1 parent cb0c2da commit 51d0dec
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 30 deletions.
5 changes: 3 additions & 2 deletions demo/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions packages/pdfeasy/src/font/vfs.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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[] = []

Expand Down
4 changes: 2 additions & 2 deletions packages/pdfeasy/src/pipe/emitter.ts
Original file line number Diff line number Diff line change
@@ -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()
})
Expand Down
12 changes: 11 additions & 1 deletion packages/pdfeasy/src/pipe/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/pdfeasy/src/pipe/setter.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/pdfeasy/src/plugins/background.ts
Original file line number Diff line number Diff line change
@@ -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__) {
Expand Down
6 changes: 3 additions & 3 deletions packages/pdfeasy/src/plugins/page.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -101,7 +101,7 @@ export const generate = (instance: pdfeasy): PluginGenerate => {
return { Text, Image }
}

export const pageHandler = (instance: pdfeasy): Promise<void> => {
export const pageHandler = (instance: PDFEasy): Promise<void> => {
return new Promise(async (response, reject) => {
const doc = instance.pdfkit

Expand Down
13 changes: 11 additions & 2 deletions packages/pdfeasy/src/runner/pdfeasy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class PDFEasy {
__BACKGROUND_RAW__: '',
},
__LAST_TYPE__: ['paragraph', 0],
__LAST_CONTENT__: {},
}

private mutateLastType = (type: ItemType) => {
Expand All @@ -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'
Expand Down Expand Up @@ -137,17 +142,19 @@ 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,
content,
this.globals,
this.optionsRun
)

this.posUpdateContent(content)
}
}

Expand All @@ -171,6 +178,7 @@ export class PDFEasy {
__BACKGROUND_RAW__: '',
},
__LAST_TYPE__: ['paragraph', 1],
__LAST_CONTENT__: {},
}
}

Expand Down Expand Up @@ -290,6 +298,7 @@ export class PDFEasy {
*/
public run = (options?: RunOptions): Promise<string> => {
this.optionsRun = options || {}
this.globals.__LAST_CONTENT__ = this.contents[0]

const runType = options?.type || 'client'

Expand Down
17 changes: 3 additions & 14 deletions packages/pdfeasy/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export interface InternalGlobals {
__BACKGROUND_RAW__: string,
},
__LAST_TYPE__: [ItemType, number]
__LAST_CONTENT__: Record<any, any>
}

export interface ImageRaw {
Expand All @@ -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<DefaultsText> {}

export interface DefaultsText {
fontSize: number
Expand All @@ -209,6 +197,7 @@ export interface DefaultsText {
go: string | undefined
bold: boolean
italic: boolean
position: { x: number, y: number }
}

export interface ContentImage {
Expand Down
1 change: 1 addition & 0 deletions packages/pdfeasy/src/utils/defines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const pdfDefaults = (): PDFEasyDefaults => {
go: undefined,
bold: false,
italic: false,
position: { x: 0, y: 0 },
},
lineBreak: {
spacing: 5,
Expand Down

0 comments on commit 51d0dec

Please sign in to comment.