Skip to content

Commit 51d0dec

Browse files
committed
feat(factory): text relative positions
1 parent cb0c2da commit 51d0dec

10 files changed

Lines changed: 40 additions & 30 deletions

File tree

demo/src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ pdfeasy.new({
1919
})
2020

2121
pdfeasy.add([
22-
{ raw: 'A simple pdf', text: {} },
22+
{ raw: 'A simple pdf', text: {}},
2323
{ lineBreak: {} },
24-
{ raw: 'using...', text: {} },
24+
{ raw: 'using...', text: { position: { x: 250, y: 0 }} },
25+
{ raw: 'hm...', text: {} },
2526
{ pageBreak: {} },
2627
{ raw: 'pdfeasy!', text: {} },
2728
...Utils.content(),

packages/pdfeasy/src/font/vfs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pdfeasy from '../runner/pdfeasy'
1+
import { PDFEasy } from '../runner/pdfeasy'
22
import path from 'path'
33
import { getBase64ByURL } from '../utils/request'
44
import { regex } from '../utils/defines'
@@ -25,7 +25,7 @@ export const setServerPath = (p: string) => {
2525
return path.join(process.cwd() + `/${p}`)
2626
}
2727

28-
export const setExternalFonts = async (instance: pdfeasy) => {
28+
export const setExternalFonts = async (instance: PDFEasy) => {
2929
if (instance.options?.advanced?.fontsPurge) {
3030
const allContentFonts: string[] = []
3131

packages/pdfeasy/src/pipe/emitter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import pdfeasy from '../runner/pdfeasy'
1+
import { PDFEasy } from '../runner/pdfeasy'
22

3-
export const onPageAdded = (instance: pdfeasy, cb: any) => {
3+
export const onPageAdded = (instance: PDFEasy, cb: any) => {
44
instance.pdfkit?.on('pageAdded', () => {
55
cb && cb()
66
})

packages/pdfeasy/src/pipe/factory.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,22 @@ export const resolveContent = async (
7272

7373
const data = embed ? ` ${raw || content.raw}` : raw || content.raw
7474

75+
const possibleLastPos = globals.__LAST_CONTENT__?.text?.position
76+
77+
let pos = style?.position
78+
? { x: app.x + style.position.x, y: app.y + style.position.y }
79+
: { x: app.x, y: app.y }
80+
if (possibleLastPos) {
81+
pos.x -= possibleLastPos.x
82+
pos.y -= possibleLastPos.y
83+
}
84+
7585
await app
7686
.font(getCorrectFontFamily(style?.font || defaults.text.font, style))
7787
.fontSize(style?.fontSize || defaults.text.fontSize)
7888
.fillColor(resolveColor(style?.color || defaults.text.color, run))
7989
.fillOpacity(style?.opacity || defaults.text.opacity)
80-
.text(data, {
90+
.text(data, pos.x, pos.y, {
8191
indent: style?.indent || defaults.text.indent,
8292
align: style?.align || defaults.text.align,
8393
paragraphGap: style?.paragraphMargin || defaults.text.paragraphMargin,

packages/pdfeasy/src/pipe/setter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { getImageRaw } from '../content/image'
22
import { regex } from '../utils/defines'
3-
import pdfeasy from '../runner/pdfeasy'
3+
import { PDFEasy } from '../runner/pdfeasy'
44

5-
export const setBackground = async (instance: pdfeasy, str: string) => {
5+
export const setBackground = async (instance: PDFEasy, str: string) => {
66
if (!instance.pdfkit) return
77

88
const kit = instance.pdfkit

packages/pdfeasy/src/plugins/background.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { setBackground } from '../pipe/setter'
2-
import pdfeasy from '../runner/pdfeasy'
2+
import { PDFEasy } from '../runner/pdfeasy'
33

4-
export const runPluginBackground = async (instance: pdfeasy) => {
4+
export const runPluginBackground = async (instance: PDFEasy) => {
55
if (instance.options?.plugins) {
66
for (const plugin of instance.options.plugins) {
77
if (plugin.background && instance.globals.__NEW_PAGE__) {

packages/pdfeasy/src/plugins/page.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import pdfeasy from '../runner/pdfeasy'
1+
import { PDFEasy } from '../runner/pdfeasy'
22
import { getCorrectFontFamily } from '../pipe/transform'
33
import { getImageRaw } from '../content/image'
44
import {
@@ -9,7 +9,7 @@ import {
99
PluginPageTextOptions,
1010
} from 'src/types'
1111

12-
export const generate = (instance: pdfeasy): PluginGenerate => {
12+
export const generate = (instance: PDFEasy): PluginGenerate => {
1313
const kit = instance.pdfkit as PDFKit.PDFDocument
1414

1515
const defaults = instance.def
@@ -101,7 +101,7 @@ export const generate = (instance: pdfeasy): PluginGenerate => {
101101
return { Text, Image }
102102
}
103103

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

packages/pdfeasy/src/runner/pdfeasy.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class PDFEasy {
9999
__BACKGROUND_RAW__: '',
100100
},
101101
__LAST_TYPE__: ['paragraph', 0],
102+
__LAST_CONTENT__: {},
102103
}
103104

104105
private mutateLastType = (type: ItemType) => {
@@ -109,6 +110,10 @@ export class PDFEasy {
109110
: [type, 1]
110111
}
111112

113+
private posUpdateContent = (content: Content) => {
114+
this.globals.__LAST_CONTENT__ = content
115+
}
116+
112117
private getType = (content: Content): ItemType => {
113118
if (content.checkbox) return 'checkbox'
114119
if (content.list) return 'list'
@@ -137,17 +142,19 @@ export class PDFEasy {
137142
for (const content of this.contents) {
138143
await runPluginBackground(this)
139144

140-
this.mutateLastType(this.getType(content))
141-
142145
if (!this.pdfkit) return
143146

147+
this.mutateLastType(this.getType(content))
148+
144149
await resolveContent(
145150
this.pdfkit,
146151
this.def,
147152
content,
148153
this.globals,
149154
this.optionsRun
150155
)
156+
157+
this.posUpdateContent(content)
151158
}
152159
}
153160

@@ -171,6 +178,7 @@ export class PDFEasy {
171178
__BACKGROUND_RAW__: '',
172179
},
173180
__LAST_TYPE__: ['paragraph', 1],
181+
__LAST_CONTENT__: {},
174182
}
175183
}
176184

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

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

packages/pdfeasy/src/types.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export interface InternalGlobals {
166166
__BACKGROUND_RAW__: string,
167167
},
168168
__LAST_TYPE__: [ItemType, number]
169+
__LAST_CONTENT__: Record<any, any>
169170
}
170171

171172
export interface ImageRaw {
@@ -181,20 +182,7 @@ export interface ExternalFont {
181182
bolditalic: string
182183
}
183184

184-
export interface ContentText {
185-
fontSize?: number
186-
font?: Fonts
187-
color?: string
188-
indent?: number
189-
align?: TextAlign
190-
paragraphMargin?: number
191-
lineHeight?: number
192-
opacity?: number
193-
destination?: string
194-
go?: string
195-
bold?: boolean
196-
italic?: boolean
197-
}
185+
export interface ContentText extends Partial<DefaultsText> {}
198186

199187
export interface DefaultsText {
200188
fontSize: number
@@ -209,6 +197,7 @@ export interface DefaultsText {
209197
go: string | undefined
210198
bold: boolean
211199
italic: boolean
200+
position: { x: number, y: number }
212201
}
213202

214203
export interface ContentImage {

packages/pdfeasy/src/utils/defines.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const pdfDefaults = (): PDFEasyDefaults => {
1515
go: undefined,
1616
bold: false,
1717
italic: false,
18+
position: { x: 0, y: 0 },
1819
},
1920
lineBreak: {
2021
spacing: 5,

0 commit comments

Comments
 (0)