Skip to content

Commit ce33aac

Browse files
committed
add genarate pdf func
1 parent 691b3e5 commit ce33aac

File tree

3 files changed

+298
-42
lines changed

3 files changed

+298
-42
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"use client"
2+
3+
export const generatePdf = async (
4+
svg: string,
5+
width: number,
6+
height: number,
7+
timeOut = 20000
8+
) => {
9+
const PDFDocument = (await import("pdfkit/js/pdfkit.standalone")).default
10+
const SVGtoPDF = (await import("svg-to-pdfkit")).default
11+
const blobstream = (await import("blob-stream")).default
12+
13+
return new Promise<string>((resolve, reject) => {
14+
const time = setTimeout(() => {
15+
reject("Timed out")
16+
}, timeOut)
17+
const doc = new PDFDocument({
18+
compress: false,
19+
size: "A4",
20+
})
21+
SVGtoPDF(doc, svg, 0, 0, {
22+
width,
23+
height,
24+
preserveAspectRatio: `xMidYMid meet`,
25+
})
26+
const stream = doc.pipe(blobstream())
27+
stream.on("finish", () => {
28+
const blob = stream.toBlob("application/pdf")
29+
clearTimeout(time)
30+
resolve(URL.createObjectURL(blob))
31+
})
32+
stream.on("")
33+
doc.end()
34+
})
35+
}

0 commit comments

Comments
 (0)