-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
150 lines (128 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import PDFDocument from 'pdfkit';
import fs from 'fs';
import fetch from 'node-fetch';
import parseColor from 'parse-color';
import getUrls from 'get-urls';
import Canvas from 'canvas';
const DPI = 75;
function pxToPt(px) {
return (px * DPI) / 100;
}
async function getGoogleFontPath(fontFamily) {
const url = `https://fonts.googleapis.com/css?family=${fontFamily}`;
const req = await fetch(url);
const text = await req.text();
const urls = getUrls(text);
return urls.values().next().value;
}
async function cropImage(element) {
// if (element.src.length >= 300) {
// return '';
// }
const image = await Canvas.loadImage(element.src);
const canvas = new Canvas.Canvas();
canvas.width = element.width;
canvas.height = element.height;
const ctx = canvas.getContext('2d');
let { cropX, cropY } = element;
const availableWidth = image.width * element.cropWidth;
const availableHeight = image.height * element.cropHeight;
const aspectRatio = element.width / element.height;
let cropAbsoluteWidth;
let cropAbsoluteHeight;
const imageRatio = availableWidth / availableHeight;
const allowScale = element.type === 'svg';
if (allowScale) {
cropAbsoluteWidth = availableWidth;
cropAbsoluteHeight = availableHeight;
} else if (aspectRatio >= imageRatio) {
cropAbsoluteWidth = availableWidth;
cropAbsoluteHeight = availableWidth / aspectRatio;
} else {
cropAbsoluteWidth = availableHeight * aspectRatio;
cropAbsoluteHeight = availableHeight;
}
ctx.drawImage(
image,
cropX * image.width,
cropY * image.height,
cropAbsoluteWidth,
cropAbsoluteHeight,
0,
0,
canvas.width,
canvas.height
);
return canvas.toDataURL();
}
async function srcToBase64(src) {
if (src.indexOf('base64') >= 0) {
return src.split('base64,')[1];
}
const res = await fetch(src);
const data = await res.buffer();
return data.toString('base64');
}
async function srcToBuffer(src) {
return Buffer.from(await srcToBase64(src), 'base64');
}
export async function jsonToPDF(json, pdfFileName) {
const fonts = {};
var doc = new PDFDocument({
size: [json.width, json.height],
});
// Stream contents to a file
doc.pipe(fs.createWriteStream(pdfFileName)).on('finish', function () {});
for (const font of json.fonts) {
// Register a font
doc.registerFont(font.fontFamily, await srcToBuffer(font.url));
fonts[font.fontFamily] = true;
}
for (const page of json.pages) {
if (page.background) {
doc.image(await srcToBuffer(page.background), 0, 0);
}
for (const child of page.children) {
doc.save();
doc.translate(child.x, child.y);
doc.rotate(child.rotation);
if (child.type === 'text') {
// doc.setFontSize(child.fontSize);
// // doc.setFontType(child.fontStyle + child.fontWeight);
// // doc.setFont(child.fontFamily);
// doc.setDrawColor(...rgb);
// doc.rect(child.x, child.y, child.width, child.height);
// doc.setTextColor(...rgb);
if (!fonts[child.fontFamily]) {
const src = await getGoogleFontPath(child.fontFamily);
doc.registerFont(child.fontFamily, await srcToBuffer(src));
fonts[child.fontFamily] = true;
}
doc.font(child.fontFamily);
doc.fontSize(child.fontSize);
// console.log(child.fill);
doc.fillColor(parseColor(child.fill).hex);
doc.text(child.text, 0, 0, {
align: child.align,
fill: child.fill,
// baseline: 'top',
// angle: child.rotation,
width: child.width,
underline: child.textDecoration.indexOf('underline') >= 0,
});
}
if (child.type === 'image' || child.type === 'svg') {
const cropped = await cropImage(child);
if (cropped) {
doc.image(await srcToBuffer(cropped), 0, 0, {
width: child.width,
height: child.height,
});
}
}
doc.restore();
}
}
// Close PDF and write file.
doc.end();
}