Skip to content

Commit dc2cabf

Browse files
committed
chore: remove svg-to-pdfkit to a native implementation
1 parent ed9d226 commit dc2cabf

5 files changed

Lines changed: 48 additions & 57 deletions

File tree

packages/pdfeasy-vite-demo/src/main.ts

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

2121
pdfeasy.add([
22-
...Utils.content(), // Utils for debug
2322
{ raw: 'Hello PDFEasy!', text: { font: 'Roboto' }}, // custom font,
2423
{ stack: [ // stack for paragraph's
2524
{ raw: 'A ', text: {}},
2625
{ raw: 'Simple', text: { bold: true, italic: true }},
2726
{ raw: ' Stack!', text: {}},
2827
]},
2928
// not recommended use this
30-
{ raw: 'Text without option!' },
29+
{ raw: '<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50"><circle cx="25" cy="25" r="20"/></svg>', svg: {}},
30+
//png
31+
{ raw: 'https://i.imgur.com/yvzMkqO.png', image: {} },
32+
...Utils.content(), // Utils for debug
3133
])
3234

3335
// or https://path/to/Roboto.ttf

packages/pdfeasy/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@
4545
"file-saver": "2.0.5",
4646
"mitt": "3.0.0",
4747
"ohmyfetch": "0.4.15",
48-
"pdfkit": "0.13.0",
49-
"svg-to-pdfkit": "0.1.8"
48+
"pdfkit": "0.13.0"
5049
},
5150
"devDependencies": {
5251
"@types/blob-stream": "0.1.30",

packages/pdfeasy/src/content/image.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,43 @@ export interface ImageRaw {
66
type: 'base64' | 'http'
77
}
88

9+
export const SvgToPNG = (raw: string): Promise<{ raw: string }> => {
10+
return new Promise((res, rej) => {
11+
const set = !raw.includes('<svg xmlns="http://www.w3.org/2000/svg"') ? raw.replace('<svg ', '<svg xmlns="http://www.w3.org/2000/svg"') : raw
12+
// convert svg to png
13+
const blob = new Blob([set], {
14+
type: 'image/svg+xml;charset=utf-8',
15+
})
16+
17+
const URL = window.URL || window.webkitURL || window
18+
19+
const blobURL = URL.createObjectURL(blob)
20+
21+
const image = new Image()
22+
image.setAttribute('crossOrigin', 'anonymous')
23+
image.onload = () => {
24+
const canvas = document.createElement('canvas')
25+
26+
canvas.width = 2000
27+
canvas.height = 2000
28+
29+
const context = canvas.getContext('2d') as CanvasRenderingContext2D
30+
31+
context.drawImage(image, 0, 0, 2000, 2000)
32+
33+
const url = canvas.toDataURL('image/png')
34+
35+
res({ raw: url })
36+
}
37+
image.onerror = () => {
38+
rej()
39+
}
40+
41+
// TODO: other blob performatic method
42+
image.src = blobURL
43+
})
44+
}
45+
946
export const getImageRaw = (raw: string): Promise<ImageRaw> => {
1047
return new Promise(async (res, rej) => {
1148
if (regex().base64(raw)) {

packages/pdfeasy/src/pipe/factory.ts

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import { PDFEasyDefaults, regex } from '../utils/defines'
1+
import { PDFEasyDefaults } from '../utils/defines'
22
import { getDataUri } from '../utils/request'
33
import { Fonts, TextAlign } from '../utils/types'
4-
import SVGtoPDF from 'svg-to-pdfkit'
54
import { getCorrectFontFamily } from './transform'
6-
import { getImageRaw } from '../content/image'
5+
import { getImageRaw, SvgToPNG } from '../content/image'
76

87
export interface ContentText {
98
fontSize?: number
@@ -136,11 +135,11 @@ export const resolveContent = async (
136135
}
137136

138137
const addImage = async () => {
139-
const style = content.image as ContentImage
138+
const style = content.svg ? content.svg : content.image as ContentImage
140139

141140
if (!content.raw) return
142141

143-
const { raw } = await getImageRaw(content.raw)
142+
const { raw } = content.svg ? await SvgToPNG(content.raw) : await getImageRaw(content.raw)
144143

145144
app.image(
146145
raw,
@@ -159,51 +158,12 @@ export const resolveContent = async (
159158
)
160159
}
161160

162-
const addSvg = async () => {
163-
const style = content.svg as ContentSVG
164-
165-
if (!content.raw) return
166-
167-
const make = async (value: string) => {
168-
await SVGtoPDF(
169-
app,
170-
value,
171-
style.x || undefined,
172-
style.y || undefined,
173-
style.size
174-
? {
175-
width: style.size?.width || undefined,
176-
height: style.size?.height || undefined,
177-
}
178-
: {}
179-
)
180-
}
181-
182-
// svg recording
183-
if (
184-
content.raw.match(
185-
/<svg\b[^>]*?(?:viewBox=\"(\b[^"]*)\")?>([\s\S]*?)<\/svg>/g
186-
)
187-
) {
188-
make(content.raw)
189-
190-
return
191-
}
192-
193-
if (content.raw.match(/https?:\/\//g)) {
194-
await getDataUri(content.raw).then((data: string) => {
195-
make(data)
196-
})
197-
}
198-
}
199-
200161
if (!content.stack && !content.text && !content.image && !content.svg) {
201162
addSimpleText()
202163
return
203164
}
204165

205166
if (content.stack) await addStack()
206167
if (content.text) await addText()
207-
if (content.image) await addImage()
208-
if (content.svg) await addSvg()
168+
if (content.image || content.svg) await addImage()
209169
}

yarn.lock

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5286,7 +5286,7 @@ pbkdf2@^3.0.3:
52865286
safe-buffer "^5.0.1"
52875287
sha.js "^2.4.8"
52885288

5289-
pdfkit@0.13.0, pdfkit@>=0.8.1:
5289+
pdfkit@0.13.0:
52905290
version "0.13.0"
52915291
resolved "https://registry.yarnpkg.com/pdfkit/-/pdfkit-0.13.0.tgz#da4c2becd63a129e3aae448fdaed4ee7be790f8f"
52925292
integrity sha512-AW79eHU5eLd2vgRDS9z3bSoi0FA+gYm+100LLosrQQMLUzOBGVOhG7ABcMFpJu7Bpg+MT74XYHi4k9EuU/9EZw==
@@ -6168,13 +6168,6 @@ supports-preserve-symlinks-flag@^1.0.0:
61686168
resolved "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz"
61696169
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==
61706170

6171-
svg-to-pdfkit@0.1.8:
6172-
version "0.1.8"
6173-
resolved "https://registry.yarnpkg.com/svg-to-pdfkit/-/svg-to-pdfkit-0.1.8.tgz#5921765922044843f0c1a5b25ec1ef8a4a33b8af"
6174-
integrity sha512-QItiGZBy5TstGy+q8mjQTMGRlDDOARXLxH+sgVm1n/LYeo0zFcQlcCh8m4zi8QxctrxB9Kue/lStc/RD5iLadQ==
6175-
dependencies:
6176-
pdfkit ">=0.8.1"
6177-
61786171
tar-stream@~2.2.0:
61796172
version "2.2.0"
61806173
resolved "https://registry.yarnpkg.com/tar-stream/-/tar-stream-2.2.0.tgz#acad84c284136b060dc3faa64474aa9aebd77287"

0 commit comments

Comments
 (0)